How to find rows in table based on multiple criteria?

119 Ansichten (letzte 30 Tage)
Shayma Al Ali
Shayma Al Ali am 27 Aug. 2021
Beantwortet: Image Analyst am 27 Aug. 2021
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 Kommentar
Kevin Holly
Kevin Holly am 27 Aug. 2021
Bearbeitet: Kevin Holly am 27 Aug. 2021
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?

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Image Analyst
Image Analyst am 27 Aug. 2021
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.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by