Info
La pregunta está cerrada. Vuélvala a abrir para editarla o responderla.
For loop
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
I'm trying to create a function to run a for loop, each time producing a matrix and storing the matrix in an array, so as to be accessed later.
However, in the code below, after the first iteration, R_1 and Radius require the previous R_2 as an input. Does this need to be declared as an input argument when i define X and R_1?
I have tried addressing it in the R_Dash function as R_2{run_number-1}, but it gives the error that R_2 is undefined.
Any help would be greatly appreciated
function R_Check = R_Check (......)
R_1 = [];
R_2 = [];
for run_number = 1:50 %run_number is used in all other functions after this
R_1{run_number} = R_dash(....run_number...)
X = X(...run_number...);
R_2{run_number} = X*R_1{run_number};
end
R_Check = R_2{50};
end
[Merged information from duplicate question]
I'll try and clear up a question i didnt ask clearly.
I'm running the function to produce a value of R_2, an n x n matrix at each iteration.
The 50th is the required result.
I need to run the previous values of R_2 in the functions in the loop but i'm having problems getting the previous values ot run in the loop.
Here's my code and the error
Hi sorry i wasnt too clear. Here's more of the relevant code.
function R_Check = R_Check (Params)
R_1 = [];
R_2 = [];
for run_number = 1:50 %run_number is used in all other functions after this
R_1{run_number} = R_dash(Params,run_number...)
X = X(Params, run_number...);
R_2{run_number} = X*R_1{run_number};
end
R_Check = R_2{50};
end
Then the R_dash function where the error occurs:
function R_dash = R_dash(Params, run_number)
if run_number == 1
R = R_0(Params);
else R = R_2{run_number-1}; %%This is where the error is reported.
.
.
.
Producing the error of:
Error in ==> R_dash at 28
else R = R_2{run_number-1};
Error in ==> R_Check at 20
R_1{run_number} = R_dash(Params, run_number);
Both X and R_dash require the previous iteration's value of R_2 to be used.
Thanks.
2 comentarios
Jan
el 3 de Mayo de 2012
Please copy the (relevant part of the) real code. The paraprasing can conceal the actual problem already. A copy of the error message is preferred also instead of a rough description.
Currently an answer requires guessing 1. where, 2. in which code and 3. which error occurs.
Walter Roberson
el 3 de Mayo de 2012
Duplicate question has been copied into here and the duplicate has been removed. You may wish to edit the above to clean it up.
Respuestas (1)
Jan
el 4 de Mayo de 2012
If the function R_dash requires the value of R_2, you have to submit it as an input argument. In the first iteration, there is no previous value, therefire this case has to be treated explicitly.
You define R_1 and R_2 as empty double matrices at first, and use it as cells later on. Although this might work (I cannot test it because your code is still not complete), it is better not to change the type of a variable implicitly.
0 comentarios
La pregunta está cerrada.
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!