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

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]'])

Risposte (3)

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 Commenti

no, i wanted 'i' to be the stored variable that is already stored in MATLAB. when i run my script currently with this coding in, it doesnt seem to do anything but skip over this function.
The why do you loop at all? What changes in each loop?
What do you mean "it doesn't seem to do anything"? Can you show how you are calling the function? Or if any errors are produced, can you post those here?
i am calling the function with
finalmatrix = for2( array, f, M )
the output of this is
pipearray(:,:,1) =
1 0
0 1
pipearray(:,:,2) =
0 0
0 0
pipearray(:,:,3) =
0 0
0 0
pipearray(:,:,1) =
1 0
0 1
pipearray(:,:,2) =
1 0
0 1
pipearray(:,:,3) =
0 0
0 0
pipearray(:,:,1) =
1 0
0 1
pipearray(:,:,2) =
1 0
0 1
pipearray(:,:,3) =
1 0
0 1
these are not unique variables?
like i said i did at one point use this
v = genvarname('pipearray', who);
val([v ' = [A B;C D]'])
at the end of the for loop and it outputted to screen pipearray1 = (an array) .. then pipearray2 = (an array) .. a worked example i looked at saved these variables is there any way to get that to work in this case, and then mutliply them all?
Did you read the FAQ that was linked in Oleg's answer? Do you understand why it is considered a bad idea to force this data into variables named pipearray1, pipearray2, etc.? You still have not explained what "it doesn't seem to do anything" means. With the data you posted, finalmatrix should come out to be the 2x2 zero matrix. If it doesn't, then could you tell us what finalmatrix is after running this code? And could you give us the array, f, and M variables so we can run it ourselves?
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?

Accedi per commentare.

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 Commento

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

Accedi per commentare.

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 Commenti

i'm a bit of a novice, do i simply replace the code with what i currently have and then put that bit used to 'call with' in the script file? (leaving these curled brackets as dots or with variables?)
Yes, but it doesn't accomplish completely what you asked. Said that I would use Sean's solutions anyways, but if you want to find out how to create array1, array2,... and understand some of the drawback, read the link to the faq.
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?

Accedi per commentare.

Richiesto:

il 18 Mag 2011

Community Treasure Hunt

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

Start Hunting!

Translated by