creating a unique variable for the outputs of a for loop?

22 ビュー (過去 30 日間)
Jamie
Jamie 2011 年 5 月 18 日
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]'])

回答 (3 件)

Andy
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 件のコメント
Andy
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
Oleg Komarov 2011 年 5 月 18 日
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.
  1 件のコメント
Jamie
Jamie 2011 年 5 月 18 日
i'm not sure what i require is on this page, i had a look through.

サインインしてコメントする。


Sean de Wolski
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);
  5 件のコメント
Sean de Wolski
Sean de Wolski 2011 年 5 月 19 日
Yes.

サインインしてコメントする。

Community Treasure Hunt

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

Start Hunting!

Translated by