How to select non-sequential values in an array?
이전 댓글 표시
Hello forum,
I am trying to select the first, fifth, and ninth values of a 12x1 matrix. I am aware of how to reference sequential values like a(1:5,1) for the first five values but cannot figure out how to properly reference the non-sequential values. Thank you!
댓글 수: 1
ADITYA RAJ ANAND
2020년 12월 2일
a ( [1 , 5 , 9] )
added spacing for clear visibility, its optional !!
채택된 답변
추가 답변 (3개)
Nestor Lora
2019년 11월 30일
49 개 추천
ans = vector([1 5 9])
댓글 수: 9
Aryan Ritwajeet Jha
2020년 4월 17일
This answer, is the first correct answer to the question. (Even if late by five years).
Tony Morrell
2020년 4월 22일
I'm doing the MATLAB Onrramp training which has the question how to do this without having explained it first, hence my search here. So, thanks for this answer.
Indices can be non-consecutive numbers. Try extracting the first, third, and sixth elements of density.
Carlos Mauricio Villamizar Mora
2020년 4월 24일
Hello, I'm also doing this the same course, I haven't find how to use variables as your index, can you help me?
Ben Vargas
2020년 6월 2일
I'm working on the same thing too! Nice!
m = density([1 3 6])
This will create a variable m that equals the first, third, and sixth element extracted from "density"
Sinhue Contreras Torres
2020년 6월 21일
I think it is the answer he was looking for. Thanks, i had the same trouble. Doing feedback i was missing the square brackets between parentheses. Now i know.
Walter Roberson
2020년 7월 24일
"This answer, is the first correct answer to the question."
Andrei's solution in 2014 was correct. Any information that is given in a Question is fair game for optimization.
Gaurav Ladha
2020년 9월 27일
Thanks for the answer. But I don't really understand the need of square brackets
Walter Roberson
2020년 9월 27일
편집: Walter Roberson
2020년 10월 30일
vector(1 5 9) would be invalid syntax
vector(1, 5, 9) would try to refer to a single value at row 1, column 5, "page" 9.
[1 5 9] or [1, 5, 9] build a list with three elements, and passing the list in to vector() means to return one value for each entry in the list.
Why is [] used to construct lists and not something like %1, 5, 9% ? Well, you need a different character to mark the beginning and end of a list so that you can nest lists. They could have arbitrarily chosen something like #1, 5, 9% but it would not have been very readable. For readability they had to use one of the available paired delimiter characters, () or [] or {} or < > or /\ .
The () pair has a lot of history for use to indicate precidence, but there is some history of use to create "tuples" so it would not be completely ruled out, but it is easier to read code if you do not need to do careful bracket counting to determine if you are using precedence or tuples.
< > has been used to construct multidimensional objects in Maple, but even Maple uses [] for ordinary lists. Historically < > delimiters have been used to delineate formal syntax such as HTML and BNF. And it gets complicated to distinguish between using these as lists or using them as relationship operators.
{} does have some history of being used for lists, but it has more history of being used for sets.
/\ has no history that I know of of being used by anyone for lists, and it would have the complication of needing to be distinguished from division.
The pair that has the most history of being used for lists is [].
It is true that Mathworks could have chosen something else than [] to indicate lists, but [] has the greatest weight of history behind it, with {} perhaps being second and < > a distant third. Using () for lists is rare outside of formal textbook discussions of tuples, with the tuples discussed in such textbooks rarely being complex enough to require precedence grouping.
Mathworks using [] was not the only option, but it was the most natural choice.
Junaid Shaikh
2020년 11월 29일
Thanks, it works.
Li Zhou
2020년 4월 25일
21 개 추천
density([1,3,6],1) if you are doing MATLAB Onramp
댓글 수: 4
DEVESH YADAV
2020년 4월 26일
seriously competition is so damn tough , everybody is doing evevrything on planet earth
Likhitha Ramini
2020년 5월 4일
density([1,3,6]) even this also works
Xiaodi Lin
2020년 7월 24일
It works, Thanks!
Irene Armenta
2020년 10월 30일
OMG thank you!
Adarsh Vijayan
2020년 4월 27일
편집: Walter Roberson
2020년 7월 24일
for i=[1 3 6]
x=density(i)
disp(x)
end
카테고리
도움말 센터 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!