How to find rows in table based on multiple criteria?

조회 수: 107 (최근 30일)
Shayma Al Ali
Shayma Al Ali 2021년 8월 27일
답변: Image Analyst 2021년 8월 27일
I have a table called shipdatatable that is 625x7. I want to select data in the table based on multiple criteria, specifically the data when the ship was in a specific location. The code I've written below is:
table_size=size(shipdatatable);
rows=table_size(1);
B1B3_index=[];
for i=1:rows
index(i)=find(shipdatatable.Lat(i) < -43.0 & shipdatatable.Lat(i) > -45.0 & shipdatatable.Lon(i) > 174.5 & shipdatatable.Lon(i) < 176);
B1B3_index=[B1B3_index;index];
end
However, I keep getting the error:
Unable to perform assignment because the indices on the left side are not compatible with
the size of the right side.
How do I go through the rows of the table and create a variable where I save the indexes of the data I want? I want to be able to use the indexes of the data I want so I can find data in other variables. I also uploaded the data below.
  댓글 수: 1
Kevin Holly
Kevin Holly 2021년 8월 27일
편집: Kevin Holly 2021년 8월 27일
table_size=size(shipdatatable);
rows=table_size(1);
B1B3_index=[];
for i=1:rows
index=find(shipdatatable.Lat(i) < -43.0 & shipdatatable.Lat(i) > -45.0 & shipdatatable.Lon(i) > 174.5 & shipdatatable.Lon(i) < 176);
B1B3_index=[B1B3_index;index];
end
Is this what you need?

댓글을 달려면 로그인하십시오.

답변 (1개)

Image Analyst
Image Analyst 2021년 8월 27일
Instead of all the for loop stuff, try this vectorized approach.
rowsToSelect = find(shipdatatable.Lat < -43.0 & shipdatatable.Lat > -45.0 & shipdatatable.Lon > 174.5 & shipdatatable.Lon < 176);
What you call B1B3_index is what I called rowsToSelect. Again, no for loop needed.
You can get rid of find() if you just want to use the logical index instead of having actual row numbers. This is what most MATLABers would do.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by