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

1 回表示 (過去 30 日間)
Masoud Taleb
Masoud Taleb 2020 年 4 月 23 日
コメント済み: BobH 2020 年 4 月 24 日
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 件のコメント
Masoud Taleb
Masoud Taleb 2020 年 4 月 24 日
Thanks for the response. It works great :)
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];
  1 件のコメント
Masoud Taleb
Masoud Taleb 2020 年 4 月 24 日
Thanks David. very nice idea :)

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by