How to attach multiple matrices to a single matrix?

조회 수: 1 (최근 30일)
niniki
niniki 2022년 3월 18일
답변: Davide Masiello 2022년 3월 18일
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.

채택된 답변

Davide Masiello
Davide Masiello 2022년 3월 18일
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'}

추가 답변 (1개)

Arif Hoq
Arif Hoq 2022년 3월 18일
편집: Arif Hoq 2022년 3월 18일
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"

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by