Vector and matrix index operations
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Ionut Anghel
am 6 Jul. 2015
Bearbeitet: Stephen23
am 25 Jun. 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 Kommentar
Akzeptierte Antwort
Keith Hooks
am 6 Jul. 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 Kommentare
John D'Errico
am 6 Jul. 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.
Weitere Antworten (2)
Thorsten
am 6 Jul. 2015
Bearbeitet: Thorsten
am 6 Jul. 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 Kommentar
Steven Lord
am 6 Jul. 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.
am 8 Jul. 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 Kommentare
Siehe auch
Kategorien
Mehr zu Argument Definitions 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!