[問題]一個bug求解

作者: ljuyentintho (小劉)   2016-06-01 00:27:16
如題
程式碼如下:
clc
clear all
R=2;
A=2;
J=3;
Fn=kron([1:R],ones(1,J*A))';
Sn=repmat(kron([1:A],ones(1,J))',R,1);
Tn=repmat([1:J]',R*A,1);
x=[repmat('X',R*A*J,1),num2str(Fn),num2str(Sn),num2str(Tn)];
X=mat2cell(x,ones(R*A*J,1),4);
randnum=8.*rand(R*A,1);
for i=1:R*A
temp=randperm(J);
X{(i-1)*J+temp(1),2}=randnum(i);
end
X(cellfun(@isempty,X))={0};
idx=find(cell2mat(X(:,2)));
Y1=cumsum(randnum(1:2:end));
Y2=cumsum(randnum(2:2:end));
y=[repmat('Y',R*A*J,1),num2str(Fn),num2str(Sn),num2str(Tn)];
Y=mat2cell(y,ones(R*A*J,1),4);
Y(idx(1:2:end),2)=mat2cell(Y1,ones(1,length(idx(1:2:end))));
Y(idx(2:2:end),2)=mat2cell(Y2,ones(1,length(idx(2:2:end))));
Y(cellfun(@isempty,Y))={0};
finalx=cell2mat(X(:,2));
finaly=cell2mat(Y(:,2));
if (finaly==0)
probabilityY=0;
else
probabilityY=0.125*exp(-(0.125*finaly));
end
finaly
probabilityY
這是其中一組輸出:
finaly =
7.8749
0
0
0
0
3.7263
9.0695
0
0
6.7079
0
0
probabilityY =
0.0467
0.1250
0.1250
0.1250
0.1250
0.0785
0.0402
0.1250
0.1250
0.0540
0.1250
0.1250
我希望當finalY這個矩陣的值為0時
probabilityY對應到的值也是0
不為0時
probabilityY=0.125*exp(-(0.125*finaly))
也寫了if跟else判斷式
但是為何finaly原本為0的地方還是被代入probabilityY=0.125*exp(-(0.125*finaly)) ?
謝謝好心的大大
作者: ejialan (eji)   2016-06-01 09:20:00
if 後面接向量要全部都是1才會是true 所以逐一判斷才對或是把if end那五行改成probabilityY=0.125*exp(-(0.125*finaly));probabilityY(~finaly)=0;
作者: ljuyentintho (小劉)   2016-06-01 19:08:00
已解決 感謝

Links booklink

Contact Us: admin [ a t ] ucptt.com