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 ?
12 次查看(过去 30 天)
显示 更早的评论
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 个评论
Sriram Ravichandran
2021-11-28
编辑:Sriram Ravichandran
2021-11-28
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.
回答(4 个)
Abhivandan Pandey
2020-6-13
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
0 个评论
Steven Lord
2020-6-13
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.
Karthick
2023-8-4
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
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!