MATLAB Answers

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

5 views (last 30 days)
Louisa Thomas
Louisa Thomas on 23 Oct 2017
Edited: Mischa Kim on 23 Oct 2017
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?

  0 Comments

Sign in to comment.

Answers (1)

Mischa Kim
Mischa Kim on 23 Oct 2017
Edited: Mischa Kim on 23 Oct 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

  0 Comments

Sign in to comment.

Sign in to answer this question.


Translated by