Most efficient way of creating variables
1 view (last 30 days)
Show older comments
James Rodriguez
on 12 Mar 2021
Commented: James Rodriguez
on 13 Mar 2021
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
0 Comments
Accepted Answer
Adam Danz
on 12 Mar 2021
Edited: Adam Danz
on 12 Mar 2021
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,:));
More Answers (0)
See Also
Categories
Find more on Logical 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!