How to display two non-consecutive column vectors

31 次查看(过去 30 天)
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 个评论
Jone Cris
Jone Cris 2021-9-16
编辑:Jone Cris 2021-9-16
Does my answer appropriate with your requirement?
b = m(:,1:2:3)

请先登录,再进行评论。

采纳的回答

Mischa Kim
Mischa Kim 2016-9-16
Use
b = m(:,[1,3])
  1 个评论
luke hodder
luke hodder 2021-2-2
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 [ ])

请先登录,再进行评论。

更多回答(6 个)

Arvind P
Arvind P 2020-3-28
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 个评论
Steven Agee
Steven Agee 2020-9-20
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
luke hodder 2021-2-2
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
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
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

madhanmohan nj
madhanmohan nj 2020-5-26
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 个评论
Marianna Nido
Marianna Nido 2020-10-17
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
ved prakash 2020-10-1
b = density([1,3,6],:)

Kevin Hedrick
Kevin Hedrick 2021-1-5
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 个评论
Othmane CHLAIKHY
Othmane CHLAIKHY 2021-2-10
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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by