Most efficient way of creating variables

조회 수: 1 (최근 30일)
James Rodriguez
James Rodriguez 2021년 3월 12일
댓글: James Rodriguez 2021년 3월 13일
I have 8 matrices, (13x10) ,which contain velocites of a flow within a channel (V1,V2.....V8). For each of these I have extarcted the lasts columns using the following :
E1 = V1(:,10);
E2 = V2(:,10);
E3 = V3(:,10);
......
E8 = V8(:,10);
I read the following https://uk.mathworks.com/matlabcentral/answers/57445-faq-how-can-i-create-variables-a1-a2-a10-in-a-loop which mentions that dynamic variable names should be avoided where possible.
In accordance, I attempted to try what the post suggested (Making one matrix with the 8 columns (E):
E = zeros(13,8); % Each column to be replaced by the last columns of V1,V2..V8
for i = [1:8];
E(i) = V(i)(:,10); % Trying to assign final columns of V1,V2..V8 to the columns of E8
end
However, I feel as if this is very far from the correct way of doing this. Any help is appreciated

채택된 답변

Adam Danz
Adam Danz 2021년 3월 12일
편집: Adam Danz 2021년 3월 12일
It's good that you're following that advice.
E = [V1(:,10), V2(:,10), . . ., Vn(:,10)]; % most efficient method of this list
If you want to use a loop, the variables have be to combined before hand,
V = {V1, V2, V3, . . ., Vn};
E = zeros(size(V1,1), numel(v));
for i = 1:numel(V)
E(:,i) = V{i}(:,10);
end
or, if the Vn matrices are all the same size,
V = cat(3, V1, V2, V3, . . ., Vn);
E = squeeze(V(:,10,:));
  댓글 수: 1
James Rodriguez
James Rodriguez 2021년 3월 13일
Thank you very much! This is very useful information and I really appreciate the alternate methods.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by