MATLAB: Standard deviation for three dimensional matrix

for loopstandard deviation

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

Best 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