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

1 次查看(过去 30 天)
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)

采纳的回答

BobH
BobH 2020-4-23
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 个评论
BobH
BobH 2020-4-24
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.

请先登录,再进行评论。

更多回答(1 个)

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

类别

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