Assign cell array of values to cell array of names

2 ビュー (過去 30 日間)
Juan Henao
Juan Henao 2015 年 8 月 5 日
コメント済み: Sean de Wolski 2015 年 8 月 6 日
Here's my question:
I have a cell array of names: {'a';'b';'c'} (example)
and a cell array of values: {1;0;1} (booleans in my case)
What I want to do is to assign the value to the name and just call it as a variable. Right now what I've been using is a for that looks for the value every time I need it.
If I need the value of a:
for i=1:length(names)
if strcmp(names{i},'a')
value_a=value{i};
break
end
end
But this takes so much time because the arrays are too long, I want that "a" contains the corresponding value.
Thank you so much!
Juan

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2015 年 8 月 5 日
編集済み: Azzi Abdelmalek 2015 年 8 月 5 日
names={'a';'b';'c'}
value= {1;0;1}
idx=ismember(names,'a')
out=value(idx)
  1 件のコメント
Juan Henao
Juan Henao 2015 年 8 月 6 日
Thank you very much, this worked for what I needed.
There are lots of things to know... awesome.

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

その他の回答 (1 件)

Sean de Wolski
Sean de Wolski 2015 年 8 月 5 日
You probably want to go with a containers.Map here:
M = containers.Map({'A';'B';'C'},1:3)
M('A')
M('B')
  2 件のコメント
Stephen23
Stephen23 2015 年 8 月 5 日
Or a simple structure?
Sean de Wolski
Sean de Wolski 2015 年 8 月 6 日
Or that...

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by