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

8 次查看(过去 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 中查找有关 Matrices and Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by