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 日

0 投票

Try this:
A((A>1)|(A<0))=5

5 件のコメント

Sudhakar Shinde
Sudhakar Shinde 2020 年 10 月 27 日
But here is a question why you would like to do this operation? At the last you know all the elements will be '5' in result if need to use both the conditions.
Liam Bouchereau
Liam Bouchereau 2020 年 10 月 27 日
Oh no, so I want to replace all elements that don't equal 1 or 0. So the array should be made up of 0's 1's and 5's like so...
A = [-1 0 4 6; 1 1 0 2; -7 0 5 0; 5 1 5 1];
would become
A = [5 0 5 5 1 1 0 5 5 0 5 0 5 1 5 1]
Sudhakar Shinde
Sudhakar Shinde 2020 年 10 月 27 日
Then you can simply use:
A((A>1)|(A<0))=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 日

0 投票

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

製品

タグ

質問済み:

2020 年 10 月 27 日

編集済み:

2020 年 10 月 27 日

Community Treasure Hunt

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

Start Hunting!

Translated by