How to make a loop run multiple times for different values of a variable.

Thanks for taking the time to read this. This is the script I'm working on:
resultX0 = cell(10, 1);
resultY0 = cell(10, 1);
resultG = cell(10, 1);
for i = 1:10
for j = 1:length(q)
[G, X0, Y0] = matrix_plantsubm(M, N, c, n, p, q(j));
resultX0{i} = X0;
resultY0{i} = Y0;
resultG{i} = G;
end
end
I would like this script to run for 10 values of q and return the answers, so I can plug them into the next function. Specifically I would like to have 10 different values of q from 0 to 1. Each result is saved in cell ResultX0 or ResultY0. After running this I have the same matrices for each cell in results. It seems like the code generates only one matrix and keeps it in 10 different cells.

6 commentaires

@POLLY: today I fixed your very badly indented code, and removed all of the pointless empty lines. Badly indented code is one way the beginners hide bugs in their code. In future you should use the MATLAB editor's default indentation (by default, or by selecting all code then pressing ctrl+i).
Today I fixed the code formatting of your question. In future you can do it yourself: first select the code text, then click the {} Code button.
Thank you! I am quite new to Matlab but will keep it mind!
Stephen23
Stephen23 le 23 Oct 2017
Modifié(e) : Stephen23 le 24 Oct 2017
@POLLY: here are a few other useful tips for writing effective MATLAB code:
Can you please upload some sample data so that we can run your code? Simply put the required variables into a Mfile and then click the paperclip button.
@Stephen Cobeldick: I have attached the mfile with variables and also the function that I run in the script above. I wanna run it and save results for q=0:0.1:1.
"It seems like the code generates only one matrix and keeps it in 10 different cells."
Your code runs okay (I used q=1:10). Lets check the output variable resultG:
>> cellfun(@isequal,repmat(resultG(:),1,N),repmat(resultG(:)',N,1))
ans =
1 0 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 1 0 0 0 0
0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 0 0 1 0 0
0 0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 0 0 1
>>
Which clearly shows that all G output matrices are different, and are not the same, as you claim.
However X0 and Y0 these will always be the same, as that is how you defined them to be: on every loop iteration the variables M, N, c, and n have exactly the same values, therefore your definitions of X0 and Y0 will always return the same matrices.
It is not clear what you expect to happen, but your code is doing exactly what you tell it to do.
POLLY
POLLY le 30 Oct 2017
Modifié(e) : POLLY le 30 Oct 2017
@Stephen I want to generate 10 different matrices for X0,Y0 and G0. Each of them depends on q=0:0.1:1(10 ways). Each matrix is supposed to be saved in individual cell.

Connectez-vous pour commenter.

Réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Question posée :

le 23 Oct 2017

Modifié(e) :

le 30 Oct 2017

Community Treasure Hunt

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

Start Hunting!

Translated by