How to attach multiple matrices to a single matrix?

1 view (last 30 days)
There are A, B, C, and D matrices of 100x1 size.
I would like to combine this data into a matrix of 400x1 size.
However, the conditions must be followed by 5x1 of each matrix.
A = [1,2,3,4,5,6,7,8,9,10....]
B = [a,b,c,d,e,f,g,h,i,j...]
C = [11,22,33,44,55,66,77,88,99...]
D = [A,B,C,D,E,F,G,H,I,J...]
Assuming that it's like this,
F = [1,2,3,4,5,a,b,c,d,e,11,22,33,44,55,A,B,C,D,E,...]
I'm going to make these results.
The method I tried was:
Inside the for loop
Use the fseek function to add 'offset=0' first and then 'offset=5'.
I saved it using the spread function.
I got the result I wanted.
I'm wondering what else you can do.

Accepted Answer

Davide Masiello
Davide Masiello on 18 Mar 2022
The code below will keep repeating the values form each array 5 by 5, i.e. it won't use only the first 5 values of each array.
In the example below I used 1x15 arrays (I don't know what's after "z" in B and C). Just replace your array and it should work
clear,clc
A = 1:15
A = 1×15
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
B = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o'}
B = 1×15 cell array
{'a'} {'b'} {'c'} {'d'} {'e'} {'f'} {'g'} {'h'} {'i'} {'j'} {'k'} {'l'} {'m'} {'n'} {'o'}
C = 11:11:165
C = 1×15
11 22 33 44 55 66 77 88 99 110 121 132 143 154 165
D = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O'}
D = 1×15 cell array
{'A'} {'B'} {'C'} {'D'} {'E'} {'F'} {'G'} {'H'} {'I'} {'J'} {'K'} {'L'} {'M'} {'N'} {'O'}
A = reshape(A,5,[]);
B = reshape(B,5,[]);
C = reshape(C,5,[]);
D = reshape(D,5,[]);
OUT = [num2cell(A);B;num2cell(C);D];
OUT = OUT(:)'
OUT = 1×60 cell array
{[1]} {[2]} {[3]} {[4]} {[5]} {'a'} {'b'} {'c'} {'d'} {'e'} {[11]} {[22]} {[33]} {[44]} {[55]} {'A'} {'B'} {'C'} {'D'} {'E'} {[6]} {[7]} {[8]} {[9]} {[10]} {'f'} {'g'} {'h'} {'i'} {'j'} {[66]} {[77]} {[88]} {[99]} {[110]} {'F'} {'G'} {'H'} {'I'} {'J'} {[11]} {[12]} {[13]} {[14]} {[15]} {'k'} {'l'} {'m'} {'n'} {'o'} {[121]} {[132]} {[143]} {[154]} {[165]} {'K'} {'L'} {'M'} {'N'} {'O'}

More Answers (1)

Arif Hoq
Arif Hoq on 18 Mar 2022
Edited: Arif Hoq on 18 Mar 2022
A = [1,2,3,4,5,6,7,8,9,10];
B = {'a','b','c','d','e','f','g','h','i','j'};
C = [11,22,33,44,55,66,77,88,99,100];
D = {'A','B','C','D','E','F','G','H','I','J'};
idx=1:5;
out2=[A(idx) string(B(idx)) C(idx) string(D(idx))]
out2 = 1×20 string array
"1" "2" "3" "4" "5" "a" "b" "c" "d" "e" "11" "22" "33" "44" "55" "A" "B" "C" "D" "E"

Community Treasure Hunt

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

Start Hunting!