Clear Filters
Clear Filters

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

8 views (last 30 days)
KALYANAPU  SRINIVAS
KALYANAPU SRINIVAS on 10 May 2017
Commented: Stephen23 on 10 May 2017
is it possible to assign the elements of matrix a to [l m]?
  2 Comments
KALYANAPU  SRINIVAS
KALYANAPU SRINIVAS on 10 May 2017
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 on 10 May 2017
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"???

Sign in to comment.

Answers (1)

KL
KL on 10 May 2017
  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 Comments
KL
KL on 10 May 2017
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 on 10 May 2017
"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.

Sign in to comment.

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!