Vector and matrix index operations

1 view (last 30 days)
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

Accepted Answer

Keith Hooks
Keith Hooks on 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 Comments
John D'Errico
John D'Errico on 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.
Ionut  Anghel
Ionut Anghel on 7 Jul 2015
Hi, Those variables are signals in a power plant defined in the early 1978. In order to get their licence some calculations are with specials codes. So, I cannot change the name of the variables. I write this part of the program to compare experimental data with their system code calculations. So, after 37 years of using those names for their parameters/variables they are very reluctant to any changes. Any way, thank you all. I proved all your solutions.

Sign in to comment.

More Answers (2)

Thorsten
Thorsten on 6 Jul 2015
Edited: Thorsten on 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 Comment
Steven Lord
Steven Lord on 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.

Sign in to comment.


F.
F. on 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...

Categories

Find more on Characters and Strings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!