How do I replace Matrix elements

35 次查看(过去 30 天)
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 个评论

请先登录,再进行评论。

更多回答(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

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by