How to specify which output will be recorded from a for loop?

1 visualizzazione (ultimi 30 giorni)
I'm working on the following piece of code:
for i = 1:length(k);
for j = length(trials);
V1 = trials(j,1) / (1 + ([k,i] * 0));
V2 = trials(j,2) / (1 + ([k,i] * trials(j,3)));
P2 = 1 / (1 + exp(-B*(V2-V1)));
end
end
length of k is 100 and length of trials is 1330.
I want the three equations to each run for the first value of k, then the second, third etc., looping for the length of k.
I want this/these loop(s) to output a 1330 x 100 matrix (trials x k), with the matrix containing only the values from the equation P2.
The loop is currently outputting three variables, each with 101 values.
How would I get this to output as above?

Risposte (1)

Mischa Kim
Mischa Kim il 23 Ott 2017
Modificato: Mischa Kim il 23 Ott 2017
Louisa, how about
P2(j,i) = 1 / (1 + exp(-B*(V2-V1)));
Careful with var names, though. i,j are also used as the imaginary unit in MATLAB. Just to be on the safe say I recommend to replace all i and j in your code by ii and jj.
for ii = 1:length(k);
for jj = length(trials);
V1 = trials(jj,1) / (1 + ([k,ii] * 0));
V2 = trials(jj,2) / (1 + ([k,ii] * trials(jj,3)));
P2(jj,ii) = 1 / (1 + exp(-B*(V2-V1)));
end
end

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by