Vector and matrix index operations

조회 수: 2 (최근 30일)
Ionut  Anghel
Ionut Anghel 2015년 7월 6일
편집: Stephen23 2019년 6월 25일
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