creating a unique variable for the outputs of a for loop?
Mostra commenti meno recenti
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)
Andy
il 18 Mag 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 Commenti
Jamie
il 18 Mag 2011
Oleg Komarov
il 18 Mag 2011
The why do you loop at all? What changes in each loop?
Andy
il 18 Mag 2011
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?
Jamie
il 19 Mag 2011
Jamie
il 19 Mag 2011
Andy
il 19 Mag 2011
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?
Jamie
il 19 Mag 2011
Oleg Komarov
il 19 Mag 2011
Take a moment to reflect on people's answers, because you already HAVE all the possible information you need.
Andy
il 19 Mag 2011
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
il 18 Mag 2011
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.
1 Commento
Jamie
il 18 Mag 2011
Sean de Wolski
il 18 Mag 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 Commenti
Jamie
il 18 Mag 2011
Oleg Komarov
il 18 Mag 2011
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.
Sean de Wolski
il 18 Mag 2011
doc function
Read up!
You need to call it with array, f, M. I was just too lazy to type those.
Jamie
il 19 Mag 2011
Sean de Wolski
il 19 Mag 2011
Yes.
Categorie
Scopri di più su Matrix Indexing in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!