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

3 ビュー (過去 30 日間)
Erin
Erin 2013 年 2 月 19 日
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 Center および File ExchangeOperators and Elementary Operations についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by