std of a logical matrix

2 次查看(过去 30 天)
Andrea
Andrea 2012-7-13
I have a 2d matrix (100 by 4 )and I want to calculate the std for each 100 by 1 non-zero vector of this matrix,. But when i wrote std(y)---. it gave me a 1 by 4 matrix as a result. It is good but I want to compute the std for non-zero elements. Then I wrote std(y(y>0))-----------. it gives me a single value as a std and not a 1 by 4 vector. Please help me regarding that issue.
Kind Regards, Andrea

采纳的回答

Kye Taylor
Kye Taylor 2012-7-13
sigmasPositive = zeros(1,4);
for j = 1:size(y2D,2)
idxOfInterest = y2D(:,j) > 0;
sigmasPositive(j) = std(y2D(idxOfInterest,j));
end
or you can avoid the loop using
z = mat2cell(y2D,size(y2D,1),ones(1,size(y2D,2)));
sigmasPositiveNoLoop = cellfun(@(c)std(c(c>0)),z);

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by