Standard deviation of a 3-d matrix

hi I have a matrix with size(y)=(200*1*4). I want to calculate the std for each (200*1) vector separately. When I used "std(y(y>0)), it gave me an value but I want in fact 4 values for each 200*1 vector size. How can I write the code to give me the std for each inner vector without "for",and ....
Thanks for your help

 채택된 답변

Kye Taylor
Kye Taylor 2012년 7월 13일

1 개 추천

If y is 200-by-1-by-4, you can get rid of the singleton dimension using
y2D = squeeze(y);
then compute the 4 different standard deviations using
the4Sigmas = std(y2D);

댓글 수: 3

Andrea
Andrea 2012년 7월 13일
thanks, it was so useful. But now another question is :
I want to calculate the std for > 0. the4Sigmas = std(y2D); give me (1 by 4) value for std but when I wrote the4Sigmas = std(y2D(y2D>0)); it gave me gain a single values while I want the std to be like before (1 by 4).
Can you please help me again?
Kye Taylor
Kye Taylor 2012년 7월 13일
편집: Kye Taylor 2012년 7월 13일
Sure, but next time, ask a brand new question.
I would then use a for loop
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);
Andrea
Andrea 2012년 7월 13일
Thanks a lot. I've already done that. but a little late. It works perfectly.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

제품

질문:

2012년 7월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by