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
ADITYA RAJ ANAND 2020 年 12 月 2 日
a ( [1 , 5 , 9] )
added spacing for clear visibility, its optional !!

サインインしてコメントする。

 採用された回答

Andrei Bobrov
Andrei Bobrov 2014 年 9 月 28 日

0 投票

out = yourvector(1:4:end);

6 件のコメント

Anirudh Bizlawan
Anirudh Bizlawan 2020 年 2 月 1 日
this answer should provide the explanation as well. This solution will only work because the vector terminates at the twelfth position. Otherwise, it will create a subset of all values starting at index 1, spaced by 4, till the end of the vector, "yourvector".
Shahram fotowat
Shahram fotowat 2020 年 3 月 31 日
combining indexing with assignment in matlab
Walter Roberson
Walter Roberson 2020 年 5 月 15 日
Erdem Yilmaz comments to Andrei Bobrov:
This answer is not correct even though it finds the correct answer.
Walter Roberson
Walter Roberson 2020 年 5 月 15 日
Erdem:
When the volunteers answer questions, they are entitled to take advantage of any information given in Question. If the question specifies 12 of something will be there, then the volunteers can write a solution that works only for exactly 12.
The solution given might not be the one expected by a TA or programmed in to a grading system, but the question did not ask,
"And please do this the way expected by my TA, George Carlin, University of Seven Words, CS010, section 17".
Rodolfo Torres
Rodolfo Torres 2020 年 5 月 17 日
Index exceeds the number of array elements (3).
It is the message I got, when I applies your solution
Walter Roberson
Walter Roberson 2020 年 5 月 17 日
YourVector = randi(9,1,12)
YourVector(1:4:end)

サインインしてコメントする。

その他の回答 (3 件)

Nestor Lora
Nestor Lora 2019 年 11 月 30 日

49 投票

ans = vector([1 5 9])

9 件のコメント

Aryan Ritwajeet Jha
Aryan Ritwajeet Jha 2020 年 4 月 17 日
This answer, is the first correct answer to the question. (Even if late by five years).
Tony Morrell
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
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
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
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
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
Gaurav Ladha 2020 年 9 月 27 日
Thanks for the answer. But I don't really understand the need of square brackets
Walter Roberson
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
Junaid Shaikh 2020 年 11 月 29 日
Thanks, it works.

サインインしてコメントする。

Li Zhou
Li Zhou 2020 年 4 月 25 日

21 投票

density([1,3,6],1) if you are doing MATLAB Onramp

4 件のコメント

DEVESH YADAV
DEVESH YADAV 2020 年 4 月 26 日
seriously competition is so damn tough , everybody is doing evevrything on planet earth
Likhitha Ramini
Likhitha Ramini 2020 年 5 月 4 日
density([1,3,6]) even this also works
Xiaodi Lin
Xiaodi Lin 2020 年 7 月 24 日
It works, Thanks!
Irene Armenta
Irene Armenta 2020 年 10 月 30 日
OMG thank you!

サインインしてコメントする。

Adarsh Vijayan
Adarsh Vijayan 2020 年 4 月 27 日
編集済み: Walter Roberson 2020 年 7 月 24 日

2 投票

for i=[1 3 6]
x=density(i)
disp(x)
end

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by