how can i create this matrix
조회 수: 1 (최근 30일)
이전 댓글 표시
hello,
i'm new to this software and i couldn't find an explanation to this
I have a matrix that is defined like this:
A(ij) , 1<=j<=7 , 1<=i<=8
if i>j then 2j-1
if i<=j then 4j-2i
how can i write it in the software?
Thanks for your help
댓글 수: 0
답변 (3개)
Stephen23
2019년 12월 30일
>> [I,J] = ndgrid(1:7,1:8);
>> X = I>J;
>> M = (2+2*~X).*J.*X - 2.*~X.*I - X
M =
-2 -2 -2 -2 -2 -2 -2 -2
1 -4 -4 -4 -4 -4 -4 -4
1 3 -6 -6 -6 -6 -6 -6
1 3 5 -8 -8 -8 -8 -8
1 3 5 7 -10 -10 -10 -10
1 3 5 7 9 -12 -12 -12
1 3 5 7 9 11 -14 -14
댓글 수: 0
shlomo shnur
2019년 12월 30일
댓글 수: 1
Stephen23
2019년 12월 30일
Your matrix dimensions are swapped: matrices are defined by rows and columns, so your original specification "A(ij) , 1<=j<=7 , 1<=i<=8" requires a 7x8 matrix, not an 8x7 matrix like in your answer.
In any case, using loops is not a particularly neat use of MATLAB.
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!