Replacing numbers in a matrix
이전 댓글 표시
I am trying to create a command that replaces all even numbers in a matrix with their square root values. I've already figured out how to determine which numbers are even. Please help.
function repEven = repEven(A)
if rem(A,2) == 0 % determines if numbers are even
end
답변 (1개)
Walter Roberson
2021년 3월 13일
편집: Walter Roberson
2021년 3월 13일
Use logical indexing.
if rem(A,2) == 0 % determines if numbers are even
That determines whether all of the numbers in A are even, not if any one of them is even.
Also, do not name your output variable the same thing as your function.
댓글 수: 3
Ryan Williams
2021년 3월 13일
A = [5 8 19]
rem(A,2) == 0
is a logical vector of true and false values.
A([false, true, false])
Ryan Williams
2021년 3월 13일
카테고리
도움말 센터 및 File Exchange에서 Library Development에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!