How to specify which output will be recorded from a for loop?
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
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
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!