Filter löschen
Filter löschen

Swapping elements from one matrix to another.

4 Ansichten (letzte 30 Tage)
shahzer rahman
shahzer rahman am 2 Mär. 2020
Bearbeitet: KSSV am 2 Mär. 2020
Hi all. I am relatively new in Matlab.
Suppose i have two matrices A= [1 2 3 -4 5 -6 8 9 10 -5 -4 -2]' and B=[12 2 3 4 5 6 -7 9 6 4 5 -1]'. Both of them are of the same order.
I want to replace the negative values in A with the corresponding values of B( the same index values).
Expected updated A = [1 2 3 4 5 6 8 9 10 4 5 -1]'.
Kindly help.
  1 Kommentar
Jintao Wu
Jintao Wu am 2 Mär. 2020
I am also new here .Now let me exchange something about the MATLAB . You can do it like this ,
[a1,a2]=find(A<0);
A[a1,a2]=B[a1,a2];
You can use the 'find' function . After that , you can try it to complete the similar issues .
Congratulations

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

KSSV
KSSV am 2 Mär. 2020
Bearbeitet: KSSV am 2 Mär. 2020
Read about logical indexing.
A(A<0) = B(A<0) ;

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