How to select the columns of a matrix which are a multiple of 5?

조회 수: 1 (최근 30일)
Ellen De Jonghe
Ellen De Jonghe 2020년 1월 9일
답변: Ellen De Jonghe 2020년 1월 9일
Hye
I don't know how to solve the following exercise:
M is a 3x5000 matrix. A student wants to select the elements on the third row in all columns which are a multiple of 5.
The student writes the following script.
N = zeros(1,1000)
for i = 1:5000
if rem(i,5) == 0
N(i/5) = M(3,i);
end
end
I have to simplify it using only N = M(...). How do I do that?
  댓글 수: 1
Guillaume
Guillaume 2020년 1월 9일
It's hard to give you any help without giving the solution away (which would be cheating).
For which i values would the if be true? Couldn't you pass these values directly as indices of M and not bother with the loop?

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

답변 (2개)

Alex Mcaulley
Alex Mcaulley 2020년 1월 9일
N = M(3,5:5:end)
  댓글 수: 2
Guillaume
Guillaume 2020년 1월 9일
In my opinion (as an academic), it'd be better if you didn't post outright solution to homework. Particularly, when it's that easy.
Alex Mcaulley
Alex Mcaulley 2020년 1월 9일
Oops you are right, I didn't realise it was homework

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


Ellen De Jonghe
Ellen De Jonghe 2020년 1월 9일
Hi guys, thanks a lot for your help!
I managed to solve exercises which are way more difficult, but my mind is still full of my previous exam stuff, so I didn't see that the solution ofr this case was so simple. Hope that makes sense ;)

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by