For Loop Indexing of matrices

1 次查看(过去 30 天)
Aidan Turner
Aidan Turner 2021-3-9
评论: Matt J 2021-3-10
I have a matrix of dimensions 36x25x355.
I would like to extract 36x25 arrays from this data and index them 1,2,3 and so on, as descrtibed in attached picture. I would like to be able to do this for any size matrix multipe times so I need to create a for loop.
As 355 does not divide wholly into 25, I would like the last array to be smaller than the others.
Any help with this woud be greatly appreciated.
  2 个评论
Jan
Jan 2021-3-9
I assume "36x25x355" is a typo. You mean 36x355, don't you?
Aidan Turner
Aidan Turner 2021-3-9
Apologies. Yes it's a 3 dimenstional NIfTI file, so in 2 dimensions 36x355

请先登录,再进行评论。

回答(2 个)

Matt J
Matt J 2021-3-9
编辑:Matt J 2021-3-9
  9 个评论
Steven Lord
Steven Lord 2021-3-10
From this additional information about what you're trying to do, splitting this matrix into cells in a cell array doesn't seem like the best option anymore. Pad your array with columns of missing data at the end until its width is a multiple of the width of the "blocks" you want to create, then reshape the data into a 3-dimensional array.
n = 7;
A = magic(n)
A = 7×7
30 39 48 1 10 19 28 38 47 7 9 18 27 29 46 6 8 17 26 35 37 5 14 16 25 34 36 45 13 15 24 33 42 44 4 21 23 32 41 43 3 12 22 31 40 49 2 11 20
paddedA = [A, NaN(n, 1)]
paddedA = 7×8
30 39 48 1 10 19 28 NaN 38 47 7 9 18 27 29 NaN 46 6 8 17 26 35 37 NaN 5 14 16 25 34 36 45 NaN 13 15 24 33 42 44 4 NaN 21 23 32 41 43 3 12 NaN 22 31 40 49 2 11 20 NaN
B = reshape(paddedA, [n 4 2])
B =
B(:,:,1) = 30 39 48 1 38 47 7 9 46 6 8 17 5 14 16 25 13 15 24 33 21 23 32 41 22 31 40 49 B(:,:,2) = 10 19 28 NaN 18 27 29 NaN 26 35 37 NaN 34 36 45 NaN 42 44 4 NaN 43 3 12 NaN 2 11 20 NaN
Now you can use the dim input argument to functions like sum and max to compute the sum or maximum in a specific dimension.
C = max(B, [], 3)
C = 7×4
30 39 48 1 38 47 29 9 46 35 37 17 34 36 45 25 42 44 24 33 43 23 32 41 22 31 40 49
Matt J
Matt J 2021-3-10
Would there be a way to calculate the max value for each point in the 36x25? For example, I would want the maximum value of the first point out of alll the reshaped matrices.
Yes, max, min, mean, median... Whatever you like:
max( reshape( yourMatrix(:,1:350), 36,25,[]) ,[],3 )

请先登录,再进行评论。


Jan
Jan 2021-3-9
X = rand(36, 355);
w = 25; % Width of the tiles
sX = size(X);
M = [repmat(w, 1, floor(sX(2) / w)), rem(sX(2), w)];
M = M(M ~= 0); % Crop last tile, if it is 0
C = mat2cell(X, sX(1), M)

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by