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
Walter Roberson 2021 年 3 月 13 日
編集済み: Walter Roberson 2021 年 3 月 13 日

0 投票

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
Ryan Williams 2021 年 3 月 13 日
Appreciate the tips, but how would I use logical indexing to determine if numbers are even?
Walter Roberson
Walter Roberson 2021 年 3 月 13 日
A = [5 8 19]
A = 1×3
5 8 19
rem(A,2) == 0
ans = 1×3 logical array
0 1 0
is a logical vector of true and false values.
A([false, true, false])
ans = 8
Ryan Williams
Ryan Williams 2021 年 3 月 13 日
Ok I understand, can you give me a hint on how to replace the numbers in my matrix with new ones?

サインインしてコメントする。

質問済み:

2021 年 3 月 13 日

コメント済み:

2021 年 3 月 13 日

Community Treasure Hunt

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

Start Hunting!

Translated by