Assign cell array of values to cell array of names
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Juan Henao
am 5 Aug. 2015
Kommentiert: Sean de Wolski
am 6 Aug. 2015
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
0 Kommentare
Akzeptierte Antwort
Azzi Abdelmalek
am 5 Aug. 2015
Bearbeitet: Azzi Abdelmalek
am 5 Aug. 2015
names={'a';'b';'c'}
value= {1;0;1}
idx=ismember(names,'a')
out=value(idx)
Weitere Antworten (1)
Sean de Wolski
am 5 Aug. 2015
You probably want to go with a containers.Map here:
M = containers.Map({'A';'B';'C'},1:3)
M('A')
M('B')
2 Kommentare
Siehe auch
Kategorien
Mehr zu Multidimensional Arrays finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!