How to pick out rows from a 2D cell array based on several criteria

3 views (last 30 days)
I have a 2D cell array. The first column is a string, the columns 3:5 are x, y, z coordinates, and the remaining columns are data. I need to be able to pick out the rows that match a specified string and coordinates and put them into new array.
The only way I figured out how to do this is (in is the input array, name is the string I want to match, xyz are the coordinates.)
a=find(strcmp(in(:,1),name));
b=find(cell2mat(in(:,3))==x);
c=find(cell2mat(in(:,4))==y);
d=find(cell2mat(in(:,5))==z);
e=intersect(a,b);
f=intersect(c,d);
g=intersect(e,f);
out=in(g,:);
Seems pretty cumbersome, is there a better way to do this?

Answers (2)

Azzi Abdelmalek
Azzi Abdelmalek on 19 Feb 2013
Edited: Azzi Abdelmalek on 19 Feb 2013
in={'a' [1] [2] [3] [100];'b' [11] [12] [13] [1000]}
x=1;y=2;z=3; % Your data
%-----------------------The code----------------------------------------
out=in(arrayfun(@(x) isequal(in{x,1},'a')& isequal(cell2mat(in(x,2:4)),[x y z]),[1:size(in,1)]'),:)

Erin
Erin on 19 Feb 2013
Thanks!

Categories

Find more on Operators and Elementary Operations 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!