# How to display two non-consecutive column vectors

83 views (last 30 days)
MIGUEL HERNANDEZ on 16 Sep 2016
Edited: Jone Cris on 16 Sep 2021
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.
Jone Cris on 16 Sep 2021
Edited: Jone Cris on 16 Sep 2021
b = m(:,1:2:3)

Mischa Kim on 16 Sep 2016
Use
b = m(:,[1,3])
luke hodder on 2 Feb 2021
Thanks for the answer; it would be good if the tutorial by this point had highlighted where to use [ ] over ( ), as it's not been completely clear about it so far. (I tried many solutions including yours but used ( ) instead of [ ])

Arvind P on 28 Mar 2020
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
1.4
3.090909
6.888
this is how you extract non consequtive indices in a column
Pratham Shah on 10 Jun 2021
as we have a row vector only "p=density([1 3 6])" will also do.

Khom Raj Thapa Magar on 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 on 1 Apr 2021
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
Martin Whybrow on 2 Apr 2021
As density is a vector, this seems to be the correct solution, it certainly worked for me.

madhanmohan nj on 26 May 2020
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 ?
Marianna Nido on 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.

ved prakash on 1 Oct 2020
b = density([1,3,6],:)
madhan ravi on 1 Oct 2020
Edited: madhan ravi on 1 Oct 2020
How’s it different from the above answers?

Kevin Hedrick on 5 Jan 2021
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.
Othmane CHLAIKHY on 10 Feb 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

### Categories

Find more on Matrix Indexing in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by