Optimizing a matrix of values
이전 댓글 표시
Hi, I need help in optimizing a matrix of values. Assuming a 3x3 matrix, I would have this matrix:

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:

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?
채택된 답변
추가 답변 (1개)
madhan ravi
2023년 12월 14일
a = zeros(3);
[m, n] = size(a);
a(~diag(1 : m)) = 1 : m * n - m;
a.'
댓글 수: 2
madhan ravi
2023년 12월 14일
편집: madhan ravi
2023년 12월 14일
a = zeros(2);
[m, n] = size(a);
a(~diag(1 : m)) = m * n - m : -1 : 1
%a(~diag(1 : m)) = flip(your_input)
madhan ravi
2023년 12월 14일
where 1 : m * n - m is your input
카테고리
도움말 센터 및 File Exchange에서 Nonlinear Optimization에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!