How to find rows in table based on multiple criteria?
    16 views (last 30 days)
  
       Show older comments
    
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 Comment
  Kevin Holly
    
 on 27 Aug 2021
				
      Edited: Kevin Holly
    
 on 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?
Answers (1)
  Image Analyst
      
      
 on 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.
0 Comments
See Also
Categories
				Find more on Tables in Help Center and File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

