Vector and matrix index operations

1 次查看(过去 30 天)
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

采纳的回答

Keith Hooks
Keith Hooks 2015-7-6
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 个评论
John D'Errico
John D'Errico 2015-7-6
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 2015-7-7
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.

请先登录,再进行评论。

更多回答(2 个)

Thorsten
Thorsten 2015-7-6
编辑:Thorsten 2015-7-6
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 个评论
Steven Lord
Steven Lord 2015-7-6
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.
F. 2015-7-8
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...

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by