I want to fix the zeros values in their same indices in certain two matrices A and B (to not be updated) in a cost function?

1 Ansicht (letzte 30 Tage)
I want to build a cost function, I want to fix the zeros values in their same indices in certain two matrices A and B (to not be updated) .
for example this is matrix A:
A = [ 0.1 0.2 0
0.2 0.8 0
0 0 0.7 ]
and I want to update this matrix by some computions, but I want to keep the zero values in their same indices and not update them.
Is there any sugestion to do that?
and in general is there a method to fix values in their same indices ( I mean do the calculation on the all values in this matrix but dont change these values; in my case the zero values)

Antworten (1)

Voss
Voss am 17 Sep. 2022
Bearbeitet: Voss am 17 Sep. 2022
A = [0.1 0.2 0; 0.2 0.8 0; 0 0 0.7]
A = 3×3
0.1000 0.2000 0 0.2000 0.8000 0 0 0 0.7000
newA = [1 2 3; 4 5 6; 7 8 9]
newA = 3×3
1 2 3 4 5 6 7 8 9
idx = A == 0;
A(~idx) = newA(~idx)
A = 3×3
1 2 0 4 5 0 0 0 9

Kategorien

Mehr zu Problem-Based Optimization Setup 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