How to find rows in table based on multiple criteria?
102 次查看(过去 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!