How to select non-sequential values in an array?

19 次查看(过去 30 天)
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!

采纳的回答

Andrei Bobrov
Andrei Bobrov 2014-9-28
out = yourvector(1:4:end);
  6 个评论
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

请先登录,再进行评论。

更多回答(3 个)

Nestor Lora
Nestor Lora 2019-11-30
ans = vector([1 5 9])
  9 个评论
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.

请先登录,再进行评论。


Li Zhou
Li Zhou 2020-4-25
density([1,3,6],1) if you are doing MATLAB Onramp
  4 个评论

请先登录,再进行评论。


Adarsh Vijayan
Adarsh Vijayan 2020-4-27
编辑:Walter Roberson 2020-7-24
for i=[1 3 6]
x=density(i)
disp(x)
end

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by