How do I replace Matrix elements

조회 수: 2 (최근 30일)
Liam Bouchereau
Liam Bouchereau 2020년 10월 27일
편집: Stephen23 2020년 10월 27일
Given a matrix A replace elements not equal 0 and not equal 1 by 5. Use disp() function to print out the resulting array.
So far I've got
A(A~=0) = 5;
disp(A);
but do not know how to make it 0 AND 1.
  댓글 수: 2
KSSV
KSSV 2020년 10월 27일
When you use ~=0 and replace those with 5, the elements whch have 1 also will be eplaced by 5 right?
Liam Bouchereau
Liam Bouchereau 2020년 10월 27일
Yeah so all elements that don't = 1 or 0 need to be replaced by the element 5

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

채택된 답변

Sudhakar Shinde
Sudhakar Shinde 2020년 10월 27일
편집: Sudhakar Shinde 2020년 10월 27일
Try this:
A((A>1)|(A<0))=5
  댓글 수: 5
Liam Bouchereau
Liam Bouchereau 2020년 10월 27일
Thankyou
Sudhakar Shinde
Sudhakar Shinde 2020년 10월 27일
Welcome

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

추가 답변 (1개)

Stephen23
Stephen23 2020년 10월 27일
편집: Stephen23 2020년 10월 27일
Use AND instead of OR:
>> A = [-1,0,4,6;1,1,0,2;-7,0,5,0;5,1,5,1]
A =
-1 0 4 6
1 1 0 2
-7 0 5 0
5 1 5 1
>> A(A~=0&A~=1) = 5
A =
5 0 5 5
1 1 0 5
5 0 5 0
5 1 5 1

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by