How can i transfer a specific number in matrix to another number along the solution matrix??

3 vues (au cours des 30 derniers jours)
Hello. I'n very novice in MATLAB, so please undersatand me..!
My queation is as title.
Let me an example.
x = [1,0,0,1] % binary
A = [-27, -10, -11, -25]
In this example, first and fourth number of x is 1.
Along x, first and fourth number of A is -27, -25.
And I want to change the bigger number of this two number to zero.
Namely, -27 < -25, so -25 transfer to 0.
The result is
A = [-27, -10, -11, 0]
if
x = [1,1,0,0] % binary
A = [-27, -10, -11, -25]
the result is
A = [-27, 0, -11, -25]
How can I code this logic??
Many masters in the world, please Help me.

Réponse acceptée

Simon Chan
Simon Chan le 13 Août 2021
Try this:
[value,~]=max(A(logical(x)));
A(A==value)=0;

Plus de réponses (2)

Walter Roberson
Walter Roberson le 13 Août 2021
x = [1,0,0,1] % binary
x = 1×4
1 0 0 1
A = [-27, -10, -11, -25]
A = 1×4
-27 -10 -11 -25
xidx = find(x==1);
[~, maxidx] = max(A(xidx));
A(xidx(maxidx)) = 0;
A
A = 1×4
-27 -10 -11 0

Chunru
Chunru le 13 Août 2021
x = [1,1,0,0]; % binary
A = [-27, -10, -11, -25];
idx = find(x);
[~, i0] = max(A(idx));
A(idx(i0)) = 0;
A
A = 1×4
-27 0 -11 -25
% A = [-27, 0, -11, -25]

Catégories

En savoir plus sur MATLAB dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by