Optimizing a matrix of values
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Allan Paolo
am 14 Dez. 2023
Beantwortet: Matt J
am 14 Dez. 2023
Hi, I need help in optimizing a matrix of values. Assuming a 3x3 matrix, I would have this matrix:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1568539/image.gif)
I needed the values of A to F be optimized. What I would usually do is to just take [A B C D E F] as an input value and call it a day. To be fair, this works decently.
My main issue is that my matrix usually changes size, ie. it becomes this:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1568544/image.gif)
I will have to change the input to [A B] this time. My question, is, is there a more elegant way of doing these things: Is there a way for me to simply plug in the two matrices without reshaping them and forcing 0 on the diagonal?
0 Kommentare
Akzeptierte Antwort
Matt J
am 14 Dez. 2023
You could define the whole matrix as unknown, but with diagonal elements bound to zero:
LB=-inf(n);
LB(1:n+1:end)=0;
UB=-LB;
M=optimvar('M',[n,n],'Lower',LB,'Upper',UB);
0 Kommentare
Weitere Antworten (1)
madhan ravi
am 14 Dez. 2023
a = zeros(3);
[m, n] = size(a);
a(~diag(1 : m)) = 1 : m * n - m;
a.'
2 Kommentare
madhan ravi
am 14 Dez. 2023
Bearbeitet: madhan ravi
am 14 Dez. 2023
a = zeros(2);
[m, n] = size(a);
a(~diag(1 : m)) = m * n - m : -1 : 1
%a(~diag(1 : m)) = flip(your_input)
Siehe auch
Kategorien
Mehr zu Operating on Diagonal Matrices 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!