Vector and matrix index operations
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Ionut Anghel
le 6 Juil 2015
Modifié(e) : Stephen23
le 25 Juin 2019
Hi, My question is about vectors and matrix: Assuming I have vector (array) of strings: myvect={'101AA21' '101AA22' '101AA23' '102AA21' '102AA22'}; I have a matrix mymatrix=magic(5); I want to define a variable from myvect for each column of mymatrix such the results should be: 101AA21=mymatrix(:,1); 101AA22=mymatrix(:,2), etc
in order to do this:
myvect={'101AA21' '101AA22' '101AA23' '102AA21' '102AA22'}; mymatrix=magic(5); mynew_vect=char(myvect);
for i=1:length(mynew_vect) mynew_vect(i)== mymatrix(:,i); end
plot(101AA21, 102AA22, '-r');
Statment mynew_vect(i)== mymatrix(:,i); it is wrong but how can be defined . The real vector is 300, matrix is 70000X300
Thank you
1 commentaire
Réponse acceptée
Keith Hooks
le 6 Juil 2015
You won't be able to vectors/matrices long with a variable name. I suggest using a table or structure for mynew_vect instead. However, the variable names you have are invalid for a structure element. They need to start with a character.
A table might have performance advantages over a structure that large. I couldn't say either way.
Using a structure:
names={'D101AA21' 'D101AA22' 'D101AA23' 'D102AA21' 'D102AA22'};
mymatrix=magic(5);
for i=1:length(names)
mynew_vect.(names{i})= mymatrix(:,i);
end
And with a table:
names={'101AA21' '101AA22' '101AA23' '102AA21' '102AA22'};
mymatrix=magic(5);
for i=1:length(names)
mynew_table.names{i}= mymatrix(:,i);
end
4 commentaires
John D'Errico
le 6 Juil 2015
That the customers want an illegal variable name is not relevant. They cannot have it. Variables cannot have names that start with a number.
And even if they decide to choose better names, I would STRONGLY suggest following Stephen's excellent recommendations here. It is just a bad idea to do what you wish to do.
Plus de réponses (2)
Thorsten
le 6 Juil 2015
Modifié(e) : Thorsten
le 6 Juil 2015
Use eval:
myvect={'101AA21' '101AA22' '101AA23' '102AA21' '102AA22'};
mymatrix=magic(5);
for i = 1:numel(myvect)
eval(['var_' myvect{i} '= mymatrix(i,:);']);
end
plot(var_101AA21, var_101AA22)
And yes, I agree that this is awful code, but the closed to be able to write something as simple as (which seems to be what wanted here, for whatever reasons):
plot(101AA21, 101AA22)
1 commentaire
Steven Lord
le 6 Juil 2015
DON'T use EVAL. Here's a slight modification of your code that avoids the EVAL while still getting close to the desired (illegal) code.
myvect={'101AA21' '101AA22' '101AA23' '102AA21' '102AA22'};
mymatrix = magic(5);
V = struct;
for k = 1:numel(myvect)
N = ['x' myvect{k}];
V.(N) = mymatrix(k, :);
end
plot(V.x101AA21, V.x101AA22)
Some prefix on the field name is necessary because MATLAB identifiers must start with a letter; I chose x.
F.
le 8 Juil 2015
What do you think about something like this :
myvect={'101AA21' '101AA22' '101AA23' '102AA21' '102AA22'};
mymatrix = magic(5);
Tmp1 = find( strcmp( myvect, '101AA21' ) );
Tmp2 = find( strcmp( myvect, '102AA21' ) );
plot( mymatrix(:,Tmp1), mymatrix(:,Tmp2) );
But perhaps, I didn't well understand what would you want to do with this variables...
0 commentaires
Voir également
Catégories
En savoir plus sur Argument Definitions dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!