How can I extract non-consecutive indices from a vector?
조회 수: 225 (최근 30일)
이전 댓글 표시
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
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
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
2020년 4월 5일
If you have a density array (d), then to extract the 1,3,6 elements:
extracted_elements=d([1,3,6]);
댓글 수: 21
Image Analyst
2023년 7월 12일
@Viktoriia observe it working without commas below:
d = 10 : 10 : 60 % Sample data vector.
extracted_elements = d([1 3 6]) % Get only some of the elements
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?
shaik mohammed ali
2024년 5월 27일
편집: shaik mohammed ali
2024년 5월 27일
yes its working thank you so much
추가 답변 (8개)
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
Girish Pal
2020년 9월 2일
p = density(1), density(3), density(6)
댓글 수: 2
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.
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]
density = data(:,2)
x = density([1,3,6])
댓글 수: 0
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]
To extract the first, third, and sixth elements of density, use [1 3 6] as an index.
density = [1 3 6]
data(density)
댓글 수: 0
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!