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

3 次查看(过去 30 天)
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?

回答(2 个)

Azzi Abdelmalek
Azzi Abdelmalek 2013-2-19
编辑:Azzi Abdelmalek 2013-2-19
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 2013-2-19
Thanks!

类别

Help CenterFile Exchange 中查找有关 Operators and Elementary Operations 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by