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

조회 수: 1 (최근 30일)
Louisa Thomas
Louisa Thomas 2017년 10월 23일
편집: Mischa Kim 2017년 10월 23일
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?

답변 (1개)

Mischa Kim
Mischa Kim 2017년 10월 23일
편집: Mischa Kim 2017년 10월 23일
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

카테고리

Help CenterFile Exchange에서 Multidimensional Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by