std of a logical matrix

1 view (last 30 days)
Andrea
Andrea on 13 Jul 2012
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

Accepted Answer

Kye Taylor
Kye Taylor on 13 Jul 2012
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);

More Answers (0)

Categories

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!