create multiple submatrices from a large matrix

5 views (last 30 days)
Dear All communtity members,
I want to write a code that creates multiple submatrices from a large matrix either through a loop or by vectorization.
The large matrix has a dimension of 54x7056 and I want to create 16 submatrices selected from different columns.
The submatrices shall be selected and repeated as following;
sub1= column, 1, 9 to 17, 25 to 33, 41 to 49.......................... and 7049 to 7056.
sub2= column, 1 to 2, 10 to 18,26 to 34, 42 to 50.................. and 7050 to 7056.
sub3= column, 1 to 3, 11 to 19,27 to 35, 43 to 51.................. and 7051 to 7056.
sub4= column, 1 to 4, 12 to 20, 28 to 36, 44 to 52................. and 7052 to 7056.
sub5= column, 1 to 5, 13 to 21, 29 to 37, 45 to 53.....7037 to 7045, and 7053 to 7056.
sub6= column, 1 to 6, 14 to 22, 30 to 38, 46 to 54.... 7038 to 7046, and 7054 to 7056.
sub7= column, 1 to 7, 15 to 23, 31 to 39, 47 to 55, .. 7039 to 7047, and 7055 to 7056.
sub8= column, 1 to 8, 16 to 24, 32 to 40, 48 to 56.... 7040 to 7048, and 7056.
sub9= column, 1 to 9, 17 to 25, 33 to 41, 49 to 57................... and 7041 to 7049.
sub10= column, 2 to 10, 18 to 26, 34 to 42, 50 to 58............... and 7042 to 7050.
sub11= column, 3 to 11, 19 to 27, 35 to 43, 51 to 59................ and 7043 to 7051.
sub12= column, 4 to 12, 20 to 28, 36 to 44, 52 to 60............... and 7044 to 7052.
sub13= column, 5 to 13, 21 to 29, 37 to 45, 53 to 61............... and 7045 to 7053.
sub14= column, 6 to 14, 22 to 30, 38 to 46, 54 to 62............... and 7046 to 7054.
sub15= column, 7 to 15, 23 to 31, 39 to 47, 55 to 63............... and 7047 to 7055.
sub16= column, 8 to 16, 24 to 32, 40 to 48, 56 to 64............... and 7048 to 7056.
I appreciate all help and tips.
Thanks in advance!

Accepted Answer

Bruno Luong
Bruno Luong on 3 Aug 2020
Edited: Bruno Luong on 3 Aug 2020
Probably better code using logical indexing
M = rand(54,7056);
[m,n] = size(M);
pattern = circshift([true(1,9) false(1,7)], -8);
pattern = repmat(pattern, 1, n/16);
pattern = pattern(1:n);
s = struct();
for r=1:16
s.(sprintf('sub%d',r)) = M(:,pattern);
pattern = circshift(pattern, 1);
end
s
  1 Comment
Askeladden2
Askeladden2 on 3 Aug 2020
Dear Bruno,
Thank you for all your help- I really appreciate it!
Both codes are working perfectly!

Sign in to comment.

More Answers (1)

Bruno Luong
Bruno Luong on 3 Aug 2020
Edited: Bruno Luong on 3 Aug 2020
M = rand(54,7056);
[m,n] = size(M);
offset = 7;
offset = mod(offset,16);
I=(-16:8:ceil(n/8)*8+offset+8);
J=-offset+(0:15)';
C = I+J;
s=struct();
for r=1:16
a = max(C(r,1:2:end),1);
b = min(C(r,2:2:end),n);
if length(a)>length(b)
a(end)=[];
end
c = arrayfun(@(a,b) a:b, a, b, 'unif', 0);
c = cat(2,c{:});
s.(sprintf('sub%d',r)) = M(:,c);
end
s
  2 Comments
Askeladden2
Askeladden2 on 3 Aug 2020
Dear Bruno,
Thank you very much for the prompt reply.
All the 16 submatrices shall have the same dimension (54 x 3969) given the sequence and repetion provided in the text above. Just for clarifications; the large matrix (54x7056) consists of 441 individual "points" in 16 "directions". I want to extract all point for 9 of the 16 directions (i.e. 54x441*9).
When I test your code submatrix 1-4 have smaller dimensions (i.e.submatrix 1 = 54x3965,.. submatrix4= 54x3968). Do you know what might be causing this?
Kind regards.
Bruno Luong
Bruno Luong on 3 Aug 2020
With editted code
s =
struct with fields:
sub1: [54×3969 double]
sub2: [54×3969 double]
sub3: [54×3969 double]
sub4: [54×3969 double]
sub5: [54×3969 double]
sub6: [54×3969 double]
sub7: [54×3969 double]
sub8: [54×3969 double]
sub9: [54×3969 double]
sub10: [54×3969 double]
sub11: [54×3969 double]
sub12: [54×3969 double]
sub13: [54×3969 double]
sub14: [54×3969 double]
sub15: [54×3969 double]
sub16: [54×3969 double]

Sign in to comment.

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!