I am trying to put zero on top of each generated column.

2 vues (au cours des 30 derniers jours)
Masoud Taleb
Masoud Taleb le 23 Avr 2020
Commenté : BobH le 24 Avr 2020
Hello,
I am trying to put zero on top of each generated column. Assuming that "A", "ds" and "ws" differ in each of my data files, I need your help to make c2 generic for every file with known "ws", "ds" and "A". In below sample, data are not that much; I do not know how to expand it for big files.
I hope someone can help in this :)
Thanks
ws = 6 %row
ds = 4 %column
A = [1;3;5;5;6;7;8;5;1;6;9;2;6;7;4;5;6;7;8;5;5;3;2;1]
for i = 1:ds
c2 = [0;A(1:ws*1);0;A(ws+1:ws*2);0;A(ws*2+1:ws*3);0;A(ws*3+1:end)]
end
g= reshape (A,ws,ds)
g2 = reshape (c2 , ws+1, ds)

Réponse acceptée

BobH
BobH le 23 Avr 2020
Your first reshape is the right approach, to arrange the values into a matrix with the right number of columns
ws = 6; %row
ds = 4; %column
A = [1;3;5;5;6;7;8;5;1;6;9;2;6;7;4;5;6;7;8;5;5;3;2;1];
g= reshape (A,ws,ds)
g =
1 8 6 8
3 5 7 5
5 1 4 5
5 6 5 3
6 9 6 2
7 2 7 1
Flip the columns, append a row of 0 at the new bottom, flip again
fg = flipud(g);
fg(end+1,:) = 0;
g0 = flipud(fg)
g0 =
0 0 0 0
1 8 6 8
3 5 7 5
5 1 4 5
5 6 5 3
6 9 6 2
7 2 7 1
  2 commentaires
Masoud Taleb
Masoud Taleb le 24 Avr 2020
Thanks for the response. It works great :)
BobH
BobH le 24 Avr 2020
David's answer is far superior, and he rightly mentions that a proper reshape depends on your original array being neatly divisible by the number of columns or rows.
Please use his answer.

Connectez-vous pour commenter.

Plus de réponses (1)

David Hill
David Hill le 23 Avr 2020
Assuming you know that mod(length(A),ws)==0
g=reshape(A,ws,[]);
g2=[zeros(1,size(g,2));g];

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by