Plot 3D array
Show older comments
Hi,
I have a cell array of 1x10 (variable k) where each of the 10 arrays are matrices of 80x31.
Now I want to make a 3D plot with x=1:80, y=k=1:10 and z my cell array.
I have tried with
surf(1:80,k,mycell{1:length(k)}(1:80,1))
but it does not work.
Any help will be very much appreciated.
Answers (1)
Anudeep Kumar
on 8 Apr 2025
Hey Auryn_
Assuming random values for your variable “k” the code below explains how to plot in 3D using “surf”:
% Initialize the cell array
numMatrices = 10; % Number of matrices (cells)
numRows = 80; % Number of rows in each matrix
numCols = 31; % Number of columns in each matrix
% Create a cell array with random numbers
k = cell(1, numMatrices);
for i = 1:numMatrices
k{i} = rand(numRows, numCols); % Each cell contains an 80x31 matrix
end
% Define the x and y axes for the plot
x = 1:numRows; % X-axis
y = 1:numMatrices; % Y-axis (for each matrix in the cell array)
% Preallocate a matrix to hold the Z values
z = zeros(numRows, numMatrices);
% Calculate the mean of each row for each matrix
for i = 1:numMatrices
% Compute the mean across columns for each row
z(:, i) = mean(k{i}, 2); % Mean of each row
end
% Create the 3D surface plot
figure;
surf(x, y, z');
xlabel('X');
ylabel('Matrix Index (Y)');
zlabel('Mean Value (Z)');
title('3D Surface Plot of Random Cell Array Data');
Here is the documentation for “surf” for reference:
Categories
Find more on Line Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!