creating a unique variable for the outputs of a for loop?
4 次查看(过去 30 天)
显示 更早的评论
my issue is that i need to have the output date of the for loop to be saved under a unique variable title (eg. pipearray1) for when M=1. and then (pipearray2) when M=2. I then need to be able to multiply all the outcomes (which will vary depending on the value of M which is defined by the input array (that a user will put in)) to leave me with a final 2x2 matrix.
f & M are defined in a script file earlier.
function [ finalmatrix ] = for2( array, f, M )
for length = 1:M
k = (2*pi*f) / array(M,4);
Y = array(M,4) / array(M,2);
const = k*array(M,1);
A = cos(const);
D = cos(const);
B = i * Y * sin (const);
C = (i/Y) * sin (const);
pipearray = [A B;C D];
end
finalmatrix = %a multiplication of all the unique variables of pipearray.
end
i have tried to use this function directly below 'pipearray = [A B;C D];' line, it returns with a varying variable name but it doesnt seem to save them all as variables in the workspace, is there anything wrong with this? and if this can be made to work how do you then multiply all the results?
v = genvarname('pipearray', who);
val([v ' = [A B;C D]'])
0 个评论
回答(3 个)
Andy
2011-5-18
Your final multiplication is matrix multiplication, right? In what order should it be done? Assuming it should be done in the order the matrices are created in the loop, you should just keep track of the running product during the loop for finalmatrix, and for pipearray, you should store the 2x2 matrices as a 3d array:
function [ finalmatrix ] = for2( array, f, M )
finalmatrix = eye(2);
pipearray = zeros(2,2,M); % preallocate
for ix = 1:M
k = (2*pi*f) / array(M,4);
Y = array(M,4) / array(M,2);
const = k*array(M,1);
A = cos(const);
D = cos(const);
B = i * Y * sin (const);
C = (i/Y) * sin (const);
finalmatrix = finalmatrix * [A B;C D];
pipearray(:,:,ix) = [A B;C D];
end
end
You named your loop variable 'length', which is a MATLAB function, so I have renamed it 'ix' above. This is a common loop variable in MATLAB. Also, you use 'i' in your loop, and 'length' doesn't show up anywhere. When you said 'i', did you mean 'length'? If so, those should now be changed to 'ix'.
9 个评论
Oleg Komarov
2011-5-19
Take a moment to reflect on people's answers, because you already HAVE all the possible information you need.
Andy
2011-5-19
Jamie, from your insistence on creating pipearray1, pipearray2, etc., I take it that you have not read the FAQ that was linked. If you had read it, you would know several reasons why this is a bad idea.
If you need all of the data stored in the 3d array called pipearray created in the code I posted before, it's there for you. If your only problem is that you need it available outside of the function, then you need to return it as output in addition to finalmatrix:
function [finalmatrix, pipearray] = for2(array,f,M)
...
Also notice that in your calling syntax:
for f=1:3
finalmatrix = for2(array,f,M);
end
you overwrite finalmatrix on each iteration of the loop, rather than storing the output. Is this intended?
Oleg Komarov
2011-5-18
See this FAQ: http://matlab.wikia.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F
The example taken from the link:
for i=1:10
eval(sprintf('A%d = [1:i]', i));
end
Run it and se what happens. I DO NOT recommend to use this method. Read the alternatives proposed in the same faq 4.6.
Sean de Wolski
2011-5-18
Since it is a function, the workspace is cleared when it's done running and only the output arguments returned.
function [finalmatrix pipearray] = for2( array, f, M )
%pass back pipearray
for length = 1:M
k = (2*pi*f) / array(M,4);
Y = array(M,4) / array(M,2);
const = k*array(M,1);
A = cos(const);
D = cos(const);
B = i * Y * sin (const);
C = (i/Y) * sin (const);
pipearray = [A B;C D];
end
finalmatrix = prod(unique(pipearray));
end
Call with:
[finalmatrix, pipearray] = for2(array, f, M);
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!