Using surf function to plot slices of a 3D matrix

AI Thread Summary
To visualize 2D slices of a 3D matrix in MATLAB, the "surf" function can be effectively used. A user encountered an issue when attempting to plot slices using "surf(A(1,:,:))", which resulted in an error because the output was a vector rather than a matrix. The solution is to use the "squeeze" function, transforming the 1x100x100 array into a 100x100 matrix with "surf(squeeze(A(1,:,:)))". This method allows for successful plotting of the desired slices, facilitating the identification of global minima within the matrix.
mikeph
Messages
1,229
Reaction score
18
Using "surf" function to plot slices of a 3D matrix

Hello

I have a 100x100x100 matrix called "A" and want to plot 2D slices of it to get a feel of where the global minima are (since there is no other way which I know of visualising it... is there?)

If I take a slice of the third component, eg. "surf(A(:,:,50))" it will plot it fine, but I cannot do this for the other two:

Code:
>>> surf(A(1,:,:))
? Error using ==> surf at 74
Z must be a matrix, not a scalar or vector.

Is there a simple workaround for this?

Thanks
 
Physics news on Phys.org


Use surf(squeeze(A(1,:,:))). Squeeze will turn the 1 x 100 x 100 array into a 100 x 100 array.
 


Brilliant- thanks very much!
 

Similar threads

Replies
1
Views
3K
Replies
3
Views
7K
Replies
1
Views
2K
Replies
5
Views
2K
Replies
2
Views
5K
Back
Top