merge six columns with zeros step by step end of each one

1 visualización (últimos 30 días)
A=[1 2 3 6 ];
B=[4 5 5 6 9 8 7 6 9 6 8 6 9];
D=[4 1 2 3 5 68 9 6 9 6 9 6 9 6 9 63 45 82 85 96 74 52 63 10 30 23 65 6 66 33 66 ];
E=[5 4 6 5 4 8 9 6 9 6 8 6];
F=[4 8 9 7 8 6 9 6 8 2 21 3 6 56 36];
G=[7 8 9 9 6 8 6 45 25 33 66 33 14 25 36 36 12];
A1=[A;zeros(size(A))]';
B1=[zeros(size(B));B]';
D1=[zeros(size(D));D]';
E1=[zeros(size(E));E]';
F1=[zeros(size(F));F]';
G1=[zeros(size(G));G]';
C=[A1;B1;D1;E1;F1;G1]
i would like to get in nx6 columns .
1 0 0 0 0 0
2 0 0 0 0 0
3 0 0 0 0 0
6 0 0 0 0 0
0 4 0 0 0 0
0 5 0 0 0 0
0 5 0 0 0 0
0 6 0 0 0 0
0 9 0 0 0 0
0 8 0 0 0 0
0 7 0 0 0 0
0 6 0 0 0 0
0 9 0 0 0 0
0 6 0 0 0 0
0 8 0 0 0 0
0 6 0 0 0 0
0 9 0 0 0 0
0 0 4 0 0 0
0 0 1
0 2
0 3
0 5
0 68
0 9
0 6
0 9
6
9
6
9
6
9
63
45
82
85
96
74
52
63
10
30
23
65
6
66
33
66
In this way need to get how to write program .Please help me in this. Thank you.

Respuesta aceptada

dpb
dpb el 7 de Ag. de 2022
Editada: dpb el 7 de Ag. de 2022
It would be far easier to write code if you would use cell array for the existing data instead of a bunch of named variables -- but, the way to build the array would be to compute the total length by adding numel() of all to determine the ending array size and allocate it...then place each vector where it belongs in the array.
N1=numel(A);
N2=numel(B);
...
N=N1+N2+...
O=zeros(N,6);
O(1:N1,1)=A;
O(N1+1:N1+N2+1,2)=B;
...
You can see the pattern; if you had all in a cell array of a given name, then you could write a loop that would address each in turn instead of writing out each explicitly...
A={[1 2 3 6 ];
[4 5 5 6 9 8 7 6 9 6 8 6 9];
[4 1 2 3 5 68 9 6 9 6 9 6 9 6 9 63 45 82 85 96 74 52 63 10 30 23 65 6 66 33 66 ];
[5 4 6 5 4 8 9 6 9 6 8 6];
[4 8 9 7 8 6 9 6 8 2 21 3 6 56 36];
[7 8 9 9 6 8 6 45 25 33 66 33 14 25 36 36 12]};
N=cellfun(@numel,A);
B=zeros(sum(N),numel(A));
i1=1;
for i=1:numel(A)
B(i1:i1+N(i)-1,i)=A{i};
i1=i1+N(i);
end
Caution, air code -- untested...
  2 comentarios
Bruno Luong
Bruno Luong el 7 de Ag. de 2022
indeed, you must specify different number of colums
B=zeros(sum(N,numel(A)))
dpb
dpb el 7 de Ag. de 2022
Editada: dpb el 7 de Ag. de 2022
Closing parens on sum() misplaced -- the autocomplete thingie got me; cursor was still inside its argument list while finishing the line -- good eyes, indeed, Bruno!!! Corrected above --
B=zeros(sum(N),numel(A));

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Characters and Strings en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by