Mix two different size arrays
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
tori lansing
am 19 Sep. 2023
Kommentiert: tori lansing
am 20 Sep. 2023
How do i mix two different size arrays i.e A=[a1 a2 a3...a12] and B=[b1 b2 b3...b9] so that the resulting one is C={a1 b1 a2 b2 a3 b3...] and the longer vector is the ones whos values continue when the shorter one is exhausted of values. I also need to do this using horizontal concatenation, horzcat, reshape, lengths, zeros and ones matricies and other commands.
0 Kommentare
Akzeptierte Antwort
Stephen23
am 19 Sep. 2023
A = rand(1,3)
B = rand(1,7)
N = min(numel(A),numel(B));
C = [reshape([A(1:N);B(1:N)],1,[]),A(N+1:end),B(N+1:end)]
5 Kommentare
Stephen23
am 20 Sep. 2023
"I want to be able to understand the different parts of the code."
It is often useful to "break" down code when debugging or trying to understand code:
A = rand(1,3) % fake data (row vector)
B = rand(1,7) % fake data (row vector)
N = min(numel(A),numel(B)) % N = shortest vector length
M = [A(1:N);B(1:N)] % 2xN matrix
R = reshape(M,1,[]) % 1x(2*N) vector... but note the order!
X = A(N+1:end) % remaining elements
Y = B(N+1:end) % remaining elements
C = [R,X,Y] % join them all together
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!