How to display two non-consecutive column vectors
Afficher commentaires plus anciens
m = [2 3 4; 5 6 7; 8 9 10]
I know how to display 1:3 or 2:3,
b = m(:,1:3)
but I am having difficulties when trying to display just first and third, not to mention when there are more columns.
8 commentaires
Abhijeet Kumar
le 20 Mar 2020
2 3 4
m = 5 6 7
8 9 10
For your given matrix 'm', lets index the 1st and 3rd element of 2nd column (i.e, 3 and 9) and assign it variable 'x'
so,
x = m([1,3],2)
Sumudu Nandakumara
le 28 Avr 2020
Thanks Kumar
Sami ullah
le 24 Juil 2020
Thanks kumar it is very helpful
Pablo Politano
le 22 Sep 2020
Thanks! I had the same question
Ömer Lütfü Dursun
le 19 Nov 2020
Thank you!
Christian Fabrice Juouadjo
le 13 Jan 2021
Thank you @kumar.
Taher Ariwala
le 3 Fév 2021
@Abhijeet Kumar u rock
Does my answer appropriate with your requirement?
b = m(:,1:2:3)
Réponse acceptée
Plus de réponses (6)
Arvind P
le 28 Mar 2020
14 votes
Try extracting the first, third, and sixth elements of density.
density=[1.4 1.8882 3.090909 4.377 5.090 6.888 7.939 8.98989 9.1225 10.36369]'
%transposed
p=density([1 3 6],:)
p
The answer is
1.4
3.090909
6.888
this is how you extract non consequtive indices in a column
5 commentaires
Diego Guisasola Plejo
le 19 Avr 2020
Why did you transpose it?
Can it be accomplished without transposing it?
Arvind P
le 19 Avr 2020
Clearly its mentioned that the values should be extracted from a column vector. By default an array is stored as a row vector in Matlab
Clara Bahoya
le 16 Sep 2020
Thank you. I've been struggling with that one. I realized that I didn't need to transpose it nor add the comma (,) and colon (:) to get it right. I'm still learning but I'm guessing it's because we had already assigned density to only the 2nd colum of the matrix (That's if they're referring to the further practice of section 5.2)
Steven Agee
le 20 Sep 2020
Thanks for the help, I was getting pretty frustrated with this part.
This would have been nice for the tutorial to explain rather than just tell you to do it.
luke hodder
le 2 Fév 2021
Agreed - at this point the course has not actually distinguished between the purposes of ( ) vs [ ], I tried all the combinations of the above but not using square brackets. Very frustrating.
Khom Raj Thapa Magar
le 10 Sep 2020
Indices can be non-consecutive numbers. Try extracting the first, third, and sixth elements of density.
Indices can be non-consecutive numbers. Try extracting the first, third, and sixth elements of density.
y = density([1 3 6],:)
KAMOOSH BABA SHAIK
le 1 Avr 2021
1 vote
Indices can be non-consecutive numbers. Try extracting the first, third, and sixth elements of density.
p = density([1,3,6])
for non-consecutive numbers
1 commentaire
Martin Whybrow
le 2 Avr 2021
As density is a vector, this seems to be the correct solution, it certainly worked for me.
madhanmohan nj
le 26 Mai 2020
0 votes
density=[1.4 1.8882 3.090909 4.377 5.090 6.888 7.939 8.98989 9.1225 10.36369]'
p = density([1,3,6], end)
p = density([1,3,6], :)
basically what is diff between line 2 & 3 ?
1 commentaire
Marianna Nido
le 17 Oct 2020
I think the diff between line 2 and three is:
-in line 2 you are extracting the 1st, the 3rd and the 6th element of the last column of density
-in line 3, you are extracting the 1st, the 3rd and the 6th element of all columns in density
In this case, the result doesn't change, since density is a vector and not a matrix.
I'm not sure about this, but i think this is the diff.
ved prakash
le 1 Oct 2020
0 votes
b = density([1,3,6],:)
1 commentaire
madhan ravi
le 1 Oct 2020
Modifié(e) : madhan ravi
le 1 Oct 2020
How’s it different from the above answers?
Kevin Hedrick
le 5 Jan 2021
0 votes
I used:
y = density(1:2:6)
Then I did a Google search to see how everyone else solved this Further Practice question and it seems I went a whole different route.
1 commentaire
Othmane CHLAIKHY
le 10 Fév 2021
no thats wrong i think your commande will create a vector named Y and containing the first, 3th and the 5th elements and not the 6th
to resolve the probleme, you need to use this type of commande
y = density([1 3 6]);
good luck
Catégories
En savoir plus sur Programming dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!