extracting values from a matrix or vector

435 ビュー (過去 30 日間)
Sofia Santos
Sofia Santos 2021 年 2 月 17 日
コメント済み: Image Analyst 2021 年 2 月 17 日
Hi!
I'm new at this and a need a litle help.
How can extract non-consecutive numbers from a vector?
For example, a = [11 22 33 44 55]
I want to extract only the 22 and the 55, b = a (...?...)
thanks

採用された回答

Fangjun Jiang
Fangjun Jiang 2021 年 2 月 17 日
b=a([2,5]), second and fifth element

その他の回答 (1 件)

Image Analyst
Image Analyst 2021 年 2 月 17 日
Consecutive in what way? Consecutive in value or index location? None of those numbers are consecutive in their values. Why those two that you chose? What criteria are you using to decide upon the second and fifth indexes?
% To extract odd numbered indexes:
b = a(1 : 2 : end);
% To extract even numbered indexes:
b = a(2 : 2 : end);
In general you can give "a" a vector of actual indexes, like [2, 4, 5] (this is called linear indexing) OR you can give a vector of logicals (or doubles that can be evaluated as logicals) that is the same size as the array, like [false, true, false, true, true] or [0, 1, 0, 1, 1].
  2 件のコメント
Sofia Santos
Sofia Santos 2021 年 2 月 17 日
thx!
(there's no critiria, it was just an exemple, i'm still learning the basics)
Image Analyst
Image Analyst 2021 年 2 月 17 日
This (indexing and extraction) will be covered in the basic course:

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

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by