Matrix variable manipulation

6 visualizzazioni (ultimi 30 giorni)
Elin
Elin il 10 Apr 2011
Hi,
I have a 3x1 string vector and a 3 by 3x3 matrix with values,
A= ['AAA';'BBB';'CCC'] B=[1 2 3;1 2 3;1 2 3]
what code can I use to create 3 new variables out of my string vector and assign them values from my matrix.
so that the result looks like this
AAA= [1;1;1] BBB= [2;2;2] CCC= [3;3;3]

Risposta accettata

the cyclist
the cyclist il 10 Apr 2011
You can use the "eval" command to do this:
A= ['AAA';'BBB';'CCC']
B=[1 2 3;1 2 3;1 2 3]
for ii = 1:3
eval([A(ii,:),'=B(:,',num2str(ii),')'])
end
If this is "real" code, as opposed to a school assignment, then this is almost certainly a poor approach to what you are trying to do. You might want to investigate "cell arrays".

Più risposte (1)

Walter Roberson
Walter Roberson il 10 Apr 2011

Categorie

Scopri di più su Variables in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by