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

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)
Thanks Kumar
Thanks kumar it is very helpful
Thanks! I had the same question
Thank you!
Thank you @kumar.
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일

20 개 추천

Use
b = m(:,[1,3])

댓글 수: 1

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일

14 개 추천

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

Why did you transpose it?
Can it be accomplished without transposing it?
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
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)
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.
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
Khom Raj Thapa Magar 2020년 9월 10일
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
KAMOOSH BABA SHAIK 2021년 4월 1일

1 개 추천

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

As density is a vector, this seems to be the correct solution, it certainly worked for me.

댓글을 달려면 로그인하십시오.

madhanmohan nj
madhanmohan nj 2020년 5월 26일

0 개 추천

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

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일

0 개 추천

b = density([1,3,6],:)

댓글 수: 1

madhan ravi
madhan ravi 2020년 10월 1일
편집: madhan ravi 2020년 10월 1일
How’s it different from the above answers?

댓글을 달려면 로그인하십시오.

Kevin Hedrick
Kevin Hedrick 2021년 1월 5일

0 개 추천

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

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

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Programming에 대해 자세히 알아보기

제품

태그

질문:

2016년 9월 16일

편집:

2021년 9월 16일

Community Treasure Hunt

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

Start Hunting!

Translated by