Solution to equation into matrix form?

조회 수: 3 (최근 30일)
John
John 2014년 10월 15일
댓글: Star Strider 2014년 10월 15일
Here is my code:
x=linspace(1,5,5)'
For c=1:2
y=x+1*c
end
I want the solution to be stored in a 5x2 matrix:
2 3
3 4
4 5
5 6
6 7
Does anyone know how to do this?

채택된 답변

Star Strider
Star Strider 2014년 10월 15일
You need to subscript it, and it works:
x=linspace(1,5,5)'
for c=1:2
y(:,c)=x+1*c;
end
It produces the matrix you posted.
  댓글 수: 2
John
John 2014년 10월 15일
Thank you, that was exactly what I wanted to do and very easy to do it! Can you explain to me what the y(:,c) is doing?
Star Strider
Star Strider 2014년 10월 15일
My pleasure!
The ‘y(:,c)’ assignment forces the output to be rows rather than columns, resulting in the (5x2) size you want. Reversing the order of the subscripts to ‘y(c,:)’ results in a (2x5) matrix. The position of the counter variable in the subscript order determines how the results of the calculations are added to the array.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by