Creating a matrix row by row with for loop how?

조회 수: 2 (최근 30일)
jessy
jessy 2013년 2월 15일
So I'm trying to figure out a way to create a matrix of x by y. Say x =[1 2 3 ] and y = [5 6 7 9 10 11]. I'm trying to get my script to shift the longest array y,to the right according to the length of x. So my matrix would look like this:
Newy =[5 6 7 8 9 10 11 0 0;
0 5 6 7 8 9 10 11 0;
0 0 5 6 7 8 9 10 11].
I know how to iterate the y array in respect to the length of x by doing :
y(mod((1:end)-i-1,end)+1)
I just can't figure out how to create a matrix row by row, I tried:
y1=[y zeros(1,length(x)-1)];
For i= 0:length(x)-1
y1(i,:) =y(mod((1:end)-i-1,end)+1);
But I keep generating errors . Any help would be much appreciated!
  댓글 수: 1
Azzi Abdelmalek
Azzi Abdelmalek 2013년 2월 15일
편집: Azzi Abdelmalek 2013년 2월 15일
y = [5 6 7 9 10 11].
Why |[5 6 7 8 9 10 11 0 0; ?

댓글을 달려면 로그인하십시오.

채택된 답변

Image Analyst
Image Analyst 2013년 2월 15일
Try this:
x =[1 2 3 ]
y = [5 6 7 9 10 11]
ly = length(y);
y1 = zeros(length(x), max(x)+ly-1)
for row = 1 : length(x)
y1(row, x(row):x(row)+ly-1) = y;
end
y1
In the command window:
y1 =
5 6 7 9 10 11 0 0
0 5 6 7 9 10 11 0
0 0 5 6 7 9 10 11
(I hope I'm not allowing you to cheat by doing your homework for you!)

추가 답변 (1개)

jessy
jessy 2013년 2월 15일
Thanks a lot!i just couldn't wrap my head around it this really helped.
  댓글 수: 1
Image Analyst
Image Analyst 2013년 2월 15일
If you're question is answered, please mark it as "Answered"

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by