フィルターのクリア

search the second column of a cell array according to the values in the first column

1 回表示 (過去 30 日間)
I'm working in MATLAB and I have the following cell array:
pippo =
'FSize' [ 10]
'MSize' [ 10]
'rho' [ 997]
'u2' [ 86.2262]
'n' [ 100]
'nimp' [ 2]
'impeller1dir' [1x66 char]
'impeller2dir' [1x66 char]
'comparedir' [1x57 char]
I would like to return the content of the cell, in the second column, which corresponds to a given value for the cell in the first column of the first row. I.e., if the input is 'nimp', I want to return 2. Is there a simple way to do this which doesn't involve looping, or is looping the only way?

採用された回答

Andrei Bobrov
Andrei Bobrov 2014 年 5 月 7 日
out = pippo{ismember(pippo(:,1),'nimp'),2};
  2 件のコメント
Roberto
Roberto 2014 年 5 月 7 日
great answer!!
Sergio Rossi
Sergio Rossi 2014 年 5 月 7 日
編集済み: Sergio Rossi 2014 年 5 月 7 日
Great! I was given this solution, too:
out = pippo{strcmp(pippo(:,1),'nimp'),2};
which does the same ( ismember is more generic, though, and works regardless of the types of elements in the first column of parameters ). However, my favourite is:
pippo=containers.Map(pippo(:,1),pippo(:,2))
You can then retrieve any value simply by accessing the container with the corresponding key:
out=pippo2('nimp');
Didn't know MATLAB had containers! Looks like a great solution. What do you think?

サインインしてコメントする。

その他の回答 (1 件)

Roberto
Roberto 2014 年 5 月 7 日
I'd recommend the use of structures:
pippo.FSize= 10;
pippo.MSize= 10;
pippo.rho= 997;
pippo.u2= 86.2262;
So when you want a member, just type:
>> pippo.FSize
ans =
10
but if it cannot be done, try to use a code like this:
pippo = {'test',4;'Some',5} ;
searched = 'some';
returned = [];
for i = 1: numel(pippo)/2
if strcmpi(pippo{i,1},searched)
returned = pippo{i,2};
end
end
disp(returned);

カテゴリ

Help Center および File ExchangeWhos についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by