I am attempting to return the mean of non zero elements in each column in my matrix;
my first attempt was:
i = 1:peopleCount;
nonZero1(i,:) = mean(nonzeros(Y(:,i)));
that returns the same result for all columns, while my second attempt:
for i = 1:peopleCount;
nonZero2(i) = mean(nonzeros(Y(:,i)));
end
returns the correct answer.
They look identical to me, but obviously, they are not.
As you pointed out the nonzeros
gives back a column vector, so you cannot expect multiple means, the same value is assigned to every element of nonZero1(:,i)
.
A possible solution without loop:
A = Y(:,i);
A(A==0) = NaN;
nonZero3 = nanmean(A,1);
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다