MATLAB Answers

Jamie
0

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

Asked by Jamie
on 18 May 2011
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 Comments

Sign in to comment.

3 Answers

Answer by Andy
on 18 May 2011

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 Comments

f can be abything that a user decides to put in for example.. 3
which then runs as a for loop in the script file which contains this function.
for f = 1:3
finalmatrix = for2( array, f, M )
end
i then need
papearray1 = [A1 B1;C1 D1]
pipearay 2 = [A2 B1;C1 D1]
pipearray3 = [A1 B1;C1 D1]
finalmatrix = pipearray1*pipearray2*pipearray3 (or depending on what M is, we will end up with the final pipearray being pipeaeeayM = [AM BM; CM DM] and so multiply all the pipearray's created)
Take a moment to reflect on people's answers, because you already HAVE all the possible information you need.
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?

Sign in to comment.


Answer by Oleg Komarov on 18 May 2011

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 Comment

i'm not sure what i require is on this page, i had a look through.

Sign in to comment.


Answer by Sean de Wolski
on 18 May 2011

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 Comments

doc function
Read up!
You need to call it with array, f, M. I was just too lazy to type those.
you know when you said the workspace is cleared.. i do not actualy need the outputs of all the individual named variables to be output to sscreen. i need them solely, inside the matrix , so i can multiply them to give me the 'finalmatrix' which i would then like to output. is this possible?, i'm presuming it saves all the unique variables while the for loop is running. then after the function is complete it then cclears the workspace?

Sign in to comment.