Replacing all even numbers in my matrix with their square root value

조회 수: 9 (최근 30일)
Cy'an Tebo
Cy'an Tebo 2021년 3월 15일
답변: KSSV 2021년 3월 15일
So I put in the code :
A=[1 4 9; 8 16 7; 3 36 4]
X=sqrt(A(rem(A,2)==0))
It did square root each even number but it put it in it's own matrix but I was trying to replace the even number with new values while still having all the other values in the matric still. How do I fix this?

채택된 답변

Mohammad Sami
Mohammad Sami 2021년 3월 15일
You need to assign it back to A to replace the even values.
A=[1 4 9; 8 16 7; 3 36 4];
i = rem(A,2)==0;
A(i) =sqrt(A(i))

추가 답변 (1개)

KSSV
KSSV 2021년 3월 15일
A=[1 4 9; 8 16 7; 3 36 4]
idx = mod(A,2) ;
A(idx==0) = sqrt(A(idx==0))

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by