In the "session 5" of MATLAB Onramp, in "Indexing of array (part 2)", it is stated that "You can also use variables as your index. Try creating a variable y, and using y as the index to data." . How to do it ?

11 views (last 30 days)
In the "session 5" of MATLAB Onramp, in "Indexing of array (part 2)", it is stated that "You can also use variables as your index. Try creating a variable y, and using y as the index to data." . How to do it ?
  1 Comment
Sriram Ravichandran
Sriram Ravichandran on 28 Nov 2021
Edited: Sriram Ravichandran on 28 Nov 2021
It is saying that you can also use variable as an index, which otherwise is an integer.
"Try creating a variable "y" with the value 8" means type the following in your live edditor:
y = 8
"and using y as the index to data" means type the following in your live edditor:
y = 8
data(y)
What this means is:
data(8)
and
y=8
data(y)
does the same thing.
But the difference is that, in the above line of code, we used an "assigned variable" (y = 8, in this cace) instead of an integer(8) as an index.

Sign in to comment.

Answers (4)

Abhivandan Pandey
Abhivandan Pandey on 13 Jun 2020
Hi Anushka,
You can assign a value to variables and then use them as index like below
y=1;
z=1;
data(y,z);
data(y,y+1);
Also you can refer to this link for more info on array-indexing
Regards,
Abhivandan

Steven Lord
Steven Lord on 13 Jun 2020
What you've done in the "Further Practice" section is what's described there, thought it isn't doing what you may expect it to do.
Indexing into an array with one numeric value (which could be a variable or a literal number) performs linear indexing. If you think of an array as being treated like one long vector, the linear index of an element in the array is which location in that long vector contains that element. In this example, each element of A is the linear index of that location.
A = reshape(1:24, [3 4 2])
A(17)
Indexing into an array with two or more numeric values performs subscripted indexing. These are row, column, page, etc. indices.
A(1, 3, 2) % row 1, column 3, page 2 of A
See this documentation page for more information on these two types of indexing and a third, logical indexing.
When you wrote data(y), even though y has two elements you're indexing into data with one expression and so that's linear indexing. The result will be elements 6 and 3 of data, not the element in row 6 and column 3 of data.
data2 = (1:10).^2
data2([6 3]) % [36 9] -- note data2 doesn't even have 6 rows.
There are functions to convert between linear indices and subscripts: see sub2ind and ind2sub.

KAMOOSH BABA SHAIK
KAMOOSH BABA SHAIK on 1 Apr 2021
y=data(y)

Karthick
Karthick on 4 Aug 2023
You can also use a variable as an index. Try creating a variable idx with the value 8 and using idx as the index to data.
Next section >
>> idx=data[variable]
idx=data[variable]
Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched delimiters.
>>
Command Window

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!