[l m]=a(1,2,3;6,7,8) in matlab..........

조회 수: 3 (최근 30일)
KALYANAPU  SRINIVAS
KALYANAPU SRINIVAS 2017년 5월 10일
댓글: Stephen23 2017년 5월 10일
is it possible to assign the elements of matrix a to [l m]?
  댓글 수: 2
KALYANAPU  SRINIVAS
KALYANAPU SRINIVAS 2017년 5월 10일
in the above question l=a[i][j] value i.e l=1 and m=a[i][j+1] value i.e m=6; and so on..... is the required answer. Please provide the code if exist
Jan
Jan 2017년 5월 10일
The question is not clear and the comment confuses me even more. What is "a[i][j]"? It is no valid Matlab syntax, but please do not let us guess if you mean C code or any other notation. Explain clearly what you want to get. "m=a[i][j+1] value i.e m=6"???

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

답변 (1개)

KL
KL 2017년 5월 10일
  1. You cannot assign two variables in one command unless it's a function return.
  2. To define an array you should use "Square braces" --> [ ]
  3. Then if you want to access the elements of a matrix, it goes like matrix_name(rowIndex, columnIndex))
For your case,
>> a = [1,2,3;6,7,8]
a =
1 2 3
6 7 8
>> l = a(1,1)
l =
1
>> m = a(2,1)
m =
6
In case you want all the values in the first row on l and second row on m, then
>> l = a(1,:)
l =
1 2 3
>> m = a(2,:)
m =
6 7 8
here : means all elements in the specified rowIndex.
  댓글 수: 3
KL
KL 2017년 5월 10일
Thanks for the links Stephen. Yes, I'm familar with cell array returns while accessing its contents (as opposed to matrices). I'm just wondering, does it really make a difference calling it array concatenation? after all, we're creating a new array by using the square brackets, I think. Having said that, I appreciate you pushing people learn the better way.
Stephen23
Stephen23 2017년 5월 10일
"does it really make a difference calling it array concatenation?"
We regularly get questions here where people want to create lists (which MATLAB does not have), and they often mistake the [] operator for a list operator. It does cause some confusion for them. I believe that clarifying the concatenation role helps beginners to understand that all numeric values are arrays (even scalar arrays (aka "numbers")), as arrays are a first-class type in MATLAB.
So while the difference might be subtle and small, I think it is a point that is worth making, in order to understand MATLAB better.

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

카테고리

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