Filter löschen
Filter löschen

How to swap cells in matrix with code

1 Ansicht (letzte 30 Tage)
Allen Antony
Allen Antony am 21 Sep. 2020
Beantwortet: Ameer Hamza am 21 Sep. 2020
I have to change the value of every cell in a matrix which is >5. However I am not sure how. I have to include the 'find' function. I have to change every value greater than 5 to 0, I am not sure how.
D=[8 1 6;3 5 7;4 9 2];
[d1,d2]=find(D>5);
d=[d1,d2];
disp(d)
Doing this gives me
>> PBTask4p3
1 1
3 2
1 3
2 3
  5 Kommentare
Allen Antony
Allen Antony am 21 Sep. 2020
I will look at it, but I do kind of need find for the task.
KSSV
KSSV am 21 Sep. 2020
Read about logical indexing.
A = rand(5) ;
idx = A>0.5 ; % logical indexing
A(idx) = 0 ;

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Ameer Hamza
Ameer Hamza am 21 Sep. 2020
Check this
D = [8 1 6;3 5 7;4 9 2];
idx = find(D > 5);
D(idx) = 0;
Result
>> D
D =
0 1 0
3 5 0
4 0 2

Weitere Antworten (0)

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by