How can I extract non-consecutive indices from a vector?

194 ビュー (過去 30 日間)
Ray Smith
Ray Smith 2020 年 4 月 5 日
回答済み: Parvin 2024 年 3 月 14 日
In 5.2 Extracting Multiple Elements, Further practice "Indices can be non-consecutive numbers. Try extracting the first, third, and sixth elements of density." How?
  14 件のコメント
Edward li
Edward li 2023 年 8 月 25 日
Could someone explain the logic behind the parenthese and the brackets. like why is it in that order and what does each mean?
Voss
Voss 2023 年 12 月 19 日
@Edward li: In this case, the parentheses are used for indexing, and the square brackets are used for array concatenation. [1,3,6] concatenates the scalars 1, 3, and 6 into a single vector, and density([1,3,6]) gets the elements of density at the indices stored in that vector.
See the Special Characters section of this page for more information:

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

採用された回答

David Hill
David Hill 2020 年 4 月 5 日
If you have a density array (d), then to extract the 1,3,6 elements:
extracted_elements=d([1,3,6]);
  20 件のコメント
Viktoriia
Viktoriia 2023 年 7 月 12 日
no it can't. It only worked for me when I used commas.
Image Analyst
Image Analyst 2023 年 7 月 12 日
@Viktoriia observe it working without commas below:
d = 10 : 10 : 60 % Sample data vector.
d = 1×6
10 20 30 40 50 60
extracted_elements = d([1 3 6]) % Get only some of the elements
extracted_elements = 1×3
10 30 60
If you execute that code on your computer what do you see? If you executed different code than above, without commas, then what was that code?

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

その他の回答 (8 件)

Kakasaheb Nikam
Kakasaheb Nikam 2020 年 5 月 12 日
density(3)
% extract third element
when we use [ ] square bracket, it extracting specific index position values.
so answer is
extracted_elements = density( [ 1, 3, 6 ] );
  2 件のコメント
MAHMUDUL FIROZ
MAHMUDUL FIROZ 2020 年 6 月 8 日
density( [ 1, 3, 6 ] )
Ume Aiman
Ume Aiman 2021 年 11 月 1 日
yess this is correct

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


shaik sahil
shaik sahil 2020 年 8 月 22 日
p=density([1,3,5])
  1 件のコメント
Md Asif Rezwan Shishir
Md Asif Rezwan Shishir 2022 年 3 月 6 日
p=density([1,3,6])
it worked for me...thanks!

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


Diogo Teixeira Fernandes
Diogo Teixeira Fernandes 2021 年 9 月 28 日
extracted_elements=density([1,3,6])
it worked for me

Girish Pal
Girish Pal 2020 年 9 月 2 日
p = density(1), density(3), density(6)
  2 件のコメント
madhan ravi
madhan ravi 2020 年 9 月 2 日
What?
Stephen23
Stephen23 2020 年 9 月 2 日
編集済み: Stephen23 2020 年 9 月 2 日
While this does literally what the question requests "...extract non-consecutive indices from a vector", it only assigns the first of the comma-separated list to p, which is unlikely to give the desired effect, nor is it likely to be what the homework task requires.

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


Jamal Ahmad
Jamal Ahmad 2021 年 6 月 19 日
p=density( [ 1, 3, 6 ] )
  1 件のコメント
madhan ravi
madhan ravi 2021 年 6 月 20 日
How's this different from other answers?

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


Harish Mirji
Harish Mirji 2022 年 2 月 14 日
density = [5 8 9 7 8 4 5 9 8 7]
density = 1×10
5 8 9 7 8 4 5 9 8 7
p = density([1 3 5])
p = 1×3
5 9 8
  1 件のコメント
Abubakarr
Abubakarr 2023 年 5 月 25 日
It worked for me, thanks.

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


Ahmed
Ahmed 2024 年 3 月 7 日
Extracting Multiple Elements
Instructions are in the task pane to the left. Complete and submit each task one at a time.
This code sets up the activity.
data = [3 0.53 4.0753 NaN;18 1.78 6.6678 2.1328;19 0.86 1.5177 3.6852;20 1.6 3.6375 8.5389;21 3 4.7243 10.157;23 6.11 9.0698 2.8739;38 2.54 5.30023 4.4508]
data = 7×4
3.0000 0.5300 4.0753 NaN 18.0000 1.7800 6.6678 2.1328 19.0000 0.8600 1.5177 3.6852 20.0000 1.6000 3.6375 8.5389 21.0000 3.0000 4.7243 10.1570 23.0000 6.1100 9.0698 2.8739 38.0000 2.5400 5.3002 4.4508
density = data(:,2)
density = 7×1
0.5300 1.7800 0.8600 1.6000 3.0000 6.1100 2.5400
x = density([1,3,6])
x = 3×1
0.5300 0.8600 6.1100

Parvin
Parvin 2024 年 3 月 14 日
This code sets up the activity.
data = [3 0.53 4.0753 NaN;18 1.78 6.6678 2.1328;19 0.86 1.5177 3.6852;20 1.6 3.6375 8.5389;21 3 4.7243 10.157;23 6.11 9.0698 2.8739;38 2.54 5.30023 4.4508]
data = 7×4
3.0000 0.5300 4.0753 NaN 18.0000 1.7800 6.6678 2.1328 19.0000 0.8600 1.5177 3.6852 20.0000 1.6000 3.6375 8.5389 21.0000 3.0000 4.7243 10.1570 23.0000 6.1100 9.0698 2.8739 38.0000 2.5400 5.3002 4.4508
To extract the first, third, and sixth elements of density, use [1 3 6] as an index.
density = [1 3 6]
density = 1×3
1 3 6
data(density)
ans = 1×3
3 19 23

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by