How to display a matrix of matrices?
12 views (last 30 days)
Show older comments
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,
0 Comments
Accepted Answer
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
disp(grid);
celldisp(grid);
% make it into a 3d numeric array (3x3x2):
G = cat(3,cellfun(@(x)x(1),grid),cellfun(@(x)x(2),grid))
% doing the same thing another way:
C = cell2mat(grid);
G = cat(3,C(1:2:end,:),C(2:2:end,:))
% 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
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!
More Answers (0)
See Also
Categories
Find more on Subplots 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!