Standard deviation for three dimensional matrix

I have a data file that has x, y, and z sets of data, dimensions 120x150x151. I need to plot the standard deviation of the numbers in x and y at each z. How can I use std() to accomplish this?
hold on
for counter=1:151
sd=std()
plot(z(counter),sd)
end
hold off

 Accepted Answer

I do not believe you need a loop for this. The 'std' function has the capability to account for direction as per the documentation :
If you would like to get the standard deviation along the x direction, use
stdYZ = std(mymatrix,1) %Assuming x changes along the columns
This returns the standard deviation along the x-direction, at each point with fixed Y and Z co-ordinates.
If you are looking for the standard deviation of an entire X-Y plane at each level Z, then I suggest using "std2" instead. Here is the documentation link:
In this case your code would look like
hold on
for counter=1:151
sd=std2(mydata(:,:,counter)) %Assuming matrix mydata is 120x150x151
plot(z(counter),sd)
end
hold off

3 Comments

I tried to do this:
StdDev=ones(1,151);
Mean=ones(1,151);
for counter=1:151
StdDev(counter)=std2(Data(:,:,counter));
Mean(counter)=mean(Data(:,:,counter));
end
figure(2)
hold on
plot(X,Mean,'k')
plot(X,Mean(X)+StdDev(X),'r--')
plot(X,Mean(X)-StdDev(X),'r--')
hold off
I received this error:
In an assignment A(I) = B, the number of elements in B and I must be the same.
Error in StdDevSSBare (line 34)
Mean(counter)=mean(Data(:,:,counter));
Like std2, use mean2 rather than mean. mean would return a 120x150 matrix and you are equating that to a matrix of size 1x151, hence the error message.
Or if you don't have the Image Processing Toolbox:
StdDev=ones(1,151);
Mean=ones(1,151);
for counter=1:151
frame = Data(:,:,counter);
StdDev(counter)=std(frame(:));
Mean(counter)=mean(frame(:));
end
figure(2)
hold on
plot(X,Mean,'k')
plot(X,Mean(X)+StdDev(X),'r--')
plot(X,Mean(X)-StdDev(X),'r--')
hold off

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 23 Jun 2015

Commented:

on 24 Jun 2015

Community Treasure Hunt

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

Start Hunting!