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

조회 수: 2 (최근 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];

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by