How to display a matrix of matrices?

12 views (last 30 days)
I have the following code:
grid = {[0;0] [0;0] [0;0];...
[50;0] [0;0] [0;50]; [0;0] [0;0] [0;0]};
and I have no idea how to display it. The image(grid) doesn't work. I'm going to be doing a diffusion calculation on these matrices and would like to see how the numbers change over time but I'm not even sure what to search on to help me.
Any ideas would be appreciated.
Thanks,

Accepted Answer

Voss
Voss on 6 Apr 2022
Your variable grid is what's called a cell array. Specifically it is a 3-by-3 cell array of 2-by-1 numeric vectors.
format compact
grid = {[0;0] [0;0] [0;0];...
[50;0] [0;0] [0;50]; [0;0] [0;0] [0;0]};
% display to the command window:
grid
grid = 3×3 cell array
{2×1 double} {2×1 double} {2×1 double} {2×1 double} {2×1 double} {2×1 double} {2×1 double} {2×1 double} {2×1 double}
disp(grid);
{2×1 double} {2×1 double} {2×1 double} {2×1 double} {2×1 double} {2×1 double} {2×1 double} {2×1 double} {2×1 double}
celldisp(grid);
grid{1,1} = 0 0 grid{2,1} = 50 0 grid{3,1} = 0 0 grid{1,2} = 0 0 grid{2,2} = 0 0 grid{3,2} = 0 0 grid{1,3} = 0 0 grid{2,3} = 0 50 grid{3,3} = 0 0
% make it into a 3d numeric array (3x3x2):
G = cat(3,cellfun(@(x)x(1),grid),cellfun(@(x)x(2),grid))
G =
G(:,:,1) = 0 0 0 50 0 0 0 0 0 G(:,:,2) = 0 0 0 0 0 50 0 0 0
% doing the same thing another way:
C = cell2mat(grid);
G = cat(3,C(1:2:end,:),C(2:2:end,:))
G =
G(:,:,1) = 0 0 0 50 0 0 0 0 0 G(:,:,2) = 0 0 0 0 0 50 0 0 0
% use image() to show both slices:
subplot(1,2,1)
image(G(:,:,1))
colorbar
subplot(1,2,2)
image(G(:,:,2))
colorbar
  2 Comments
Voss
Voss on 6 Apr 2022
You're welcome!
If you have any questions about anything in my answer, please let me know. Otherwise, if the answer answers your question, please click 'Accept this Answer'. Thanks!

Sign in to comment.

More Answers (0)

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!