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 (최근 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
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
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

Steven Lord
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.

KAMOOSH BABA SHAIK
KAMOOSH BABA SHAIK 2021년 4월 1일
y=data(y)

Karthick
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

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by