How can I extract non-consecutive indices from a vector?
Afficher commentaires plus anciens
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?
15 commentaires
Ahmad Ali
le 27 Mai 2020
p = density ([1,3,6])
Panagiotis Papias
le 19 Nov 2020
Thank you @Ahmad Ali
kavan dave
le 26 Nov 2020
thank you @Ahmad Ali
Hardik Chauhan
le 18 Avr 2021
there is also a long way around
p=[density(1) density(3) density(6)]
Jingliang Zhang
le 21 Mai 2021
p = transpose([density(1) density(3) density(6)])
Namratha priya S R
le 24 Mai 2021
just
p=density([1 3 6])
RAJNISH KUMAR
le 3 Juin 2021
Nice 👌👌
Arjun Sabu
le 22 Oct 2021
Great
Kevin Kipkorir
le 17 Mar 2022
thank you
Min
le 14 Juil 2022
It's helpful!
Tadele
le 3 Nov 2022
Thank you
thanks@Ahmad Ali
Edward li
le 25 Août 2023
Could someone explain the logic behind the parenthese and the brackets. like why is it in that order and what does each mean?
Voss
le 19 Déc 2023
@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:
Jeronimo
le 7 Juil 2025
Thanks Ahmad Ali
Réponse acceptée
Plus de réponses (8)
Kakasaheb Nikam
le 12 Mai 2020
11 votes
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 commentaires
MAHMUDUL FIROZ
le 8 Juin 2020
density( [ 1, 3, 6 ] )
Ume Aiman
le 1 Nov 2021
yess this is correct
shaik sahil
le 22 Août 2020
3 votes
p=density([1,3,5])
1 commentaire
Md Asif Rezwan Shishir
le 6 Mar 2022
p=density([1,3,6])
it worked for me...thanks!
Diogo Teixeira Fernandes
le 28 Sep 2021
extracted_elements=density([1,3,6])
it worked for me
Girish Pal
le 2 Sep 2020
0 votes
p = density(1), density(3), density(6)
2 commentaires
madhan ravi
le 2 Sep 2020
What?
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
le 19 Juin 2021
0 votes
p=density( [ 1, 3, 6 ] )
1 commentaire
madhan ravi
le 20 Juin 2021
How's this different from other answers?
density = [5 8 9 7 8 4 5 9 8 7]
p = density([1 3 5])
1 commentaire
Abubakarr
le 25 Mai 2023
It worked for me, thanks.
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])
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)
Catégories
En savoir plus sur Introduction to Installation and Licensing dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!