how to create a matrix in matlab
Mostrar comentarios más antiguos
I have A=[1 3; 2 4]; and B=[5 6; 7 8];
I would like to create this two matrix line by line in C :
C=[1 3;5 6; 2 4; 7 8];
5 comentarios
Sena Koçak
el 1 de Feb. de 2022
By writing, C = [A(1,:);B(1,:);A(2,:);A(2,:)]
marwa hajji
el 1 de Feb. de 2022
Sena Koçak
el 1 de Feb. de 2022
I wrote on my phone, so I hope there is error in that. There are simply two case, odd and even number of total row. Maybe there exists a simple solution, but it works :)
[sizeRowA sizeColA] = size(A); [sizeRowB sizeColB] = size(B);
C = zeros(sizeRowA + sizeRowB, sizeColA);
if (rem((sizeRowA + sizeRowB),2) == 0) for i = 1:((sizeRowA+sizeRowB)/2) C(2*i-1,:) = A(i,:); C(2*i,:) = B(i,:); end else for i = 1:((sizeRowA+sizeRowB-1)/2) C(2*i-1,:) = A(i,:); C(2*i,:) = B(i,:); end C(sizeRowA+sizeRowB,:) = A(sizeRowA,:) end
marwa hajji
el 2 de Feb. de 2022
Image Analyst
el 2 de Feb. de 2022
@marwa hajji did you see my Answer below (scroll down to the official Answers section, not up here in the comments section which is supposed to be used to ask the original poster for clarification)?
Respuestas (2)
Benjamin Thompson
el 1 de Feb. de 2022
0 votos
A couple different ways:
>> A = [1 3; 2 4]
A =
1 3
2 4
>> B = [5 6; 7 8]
B =
5 6
7 8
>> C = A
C =
1 3
2 4
>> C = [C; B]
C =
1 3
2 4
5 6
7 8
>> C = [A; B]
C =
1 3
2 4
5 6
7 8
1 comentario
This
C =
1 3
2 4
5 6
7 8
is not what he wanted. He said he wants
C=[1 3;5 6; 2 4; 7 8]
Try this:
A=[1 3; 2 4]
B=[5 6; 7 8]
% What is desired:
C = [1 3;5 6; 2 4; 7 8]
% My code
C2 = [A(1,:); B(1, :); A(2,:); B(2,:)]
If you need it generalized to interleave a different number of rows than 2, or if A and B might have different numbers of rows, then it would be more complicated.
Categorías
Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!