How to find rows in table based on multiple criteria?
107 ビュー (過去 30 日間)
古いコメントを表示
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
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
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.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!