Using surf function to plot slices of a 3D matrix

In summary, the conversation discusses using the "surf" function to plot slices of a 3D matrix. The individual has a 100x100x100 matrix and wants to plot 2D slices to visualize the global minima. They encounter an error when attempting to take a slice of the first two components and are looking for a workaround. The solution is to use the "squeeze" function to transform the array into a suitable format for plotting.
  • #1
mikeph
1,235
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
  • #2


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


Brilliant- thanks very much!
 

Related to Using surf function to plot slices of a 3D matrix

1. What is the purpose of using the surf function to plot slices of a 3D matrix?

The surf function is commonly used in scientific data visualization to plot three-dimensional (3D) data in a 2D format. It allows for the visualization of a 3D matrix or array by creating a surface plot, where the height or color of the surface corresponds to the values of the matrix.

2. How do I use the surf function to plot slices of a 3D matrix in MATLAB?

To use the surf function in MATLAB, you first need to define your 3D matrix or array. Then, use the surf function and specify the matrix as the input, along with any desired formatting options such as color or shading. The resulting plot will show slices of your 3D matrix in a 2D format.

3. Can I customize the appearance of the surf plot?

Yes, the surf function allows for various customization options such as changing the color and shading of the surface, adding labels and titles, and adjusting the viewing angle. These options can help enhance the visualization and make it more informative.

4. What type of data is best suited for visualization using the surf function?

The surf function is best used for visualizing continuous and numerical data, such as temperature, elevation, or chemical concentrations. It can also be used for discrete data, but the resulting surface may appear more jagged or blocky.

5. Are there any alternative functions to surf for visualizing a 3D matrix?

Yes, there are other functions such as mesh and contour that can also be used to visualize 3D data. However, the surf function is specifically designed for creating surface plots, making it a popular choice for visualizing 3D matrices in scientific research and data analysis.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
888
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
694
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
6K
Back
Top