Iterating through a matrix

조회 수: 11 (최근 30일)
Ashley Birkhimer
Ashley Birkhimer 2020년 11월 4일
편집: Ameer Hamza 2020년 11월 4일
I am trying to go from a 4x7 zero matrix, and change odd number indices in a matrix to 30 and even numbered indices to 45 using a loop. Does anyone know how to do this?
  댓글 수: 1
Rik
Rik 2020년 11월 4일
Why do you want a loop? And do you consider (2,3) even or odd? This is small enough that for 4x7 you could write out the intended output.

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

답변 (1개)

Ameer Hamza
Ameer Hamza 2020년 11월 4일
편집: Ameer Hamza 2020년 11월 4일
Since this seems like a homework question so I won't give the solution, but here is a hint. You can use mod() function to check if a number is even or odd.
And use for-loop to iterate over a matrix
M; % matrix
for i = 1:numel(M)
if mod(i, ???)
M(i) = ???
else
M(i) = ???
end
end

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by