How can i use elements with equal indexes?

1 回表示 (過去 30 日間)
Azime Beyza Ari
Azime Beyza Ari 2022 年 4 月 10 日
コメント済み: Azime Beyza Ari 2022 年 4 月 10 日
Hello everyone, I am working on an constraint where i will only use the elements of an matrix if their indexes are equal like;
for i=1:36
for j=1:36
if i=j
M*x(i,j)-y(j)>=0
else
end
end
end
my matrix has 36x36 dimensions so need to go from 1 to 36.
this is what i did for y ;
y(:,1)
y is 36x1 so this works for y.
i i can not use any loops or conditions i am trying to write this only in one line! if there is a little tip like
x(do something here);
that is what i need.
Hope i can make what i need clear.
Thank you in advance!

採用された回答

Matt J
Matt J 2022 年 4 月 10 日
problem.Constraints.diagcon=diag(x)*M-y>=0;
  1 件のコメント
Azime Beyza Ari
Azime Beyza Ari 2022 年 4 月 10 日
Thank you this was what i needed!

サインインしてコメントする。

その他の回答 (1 件)

Torsten
Torsten 2022 年 4 月 10 日
編集済み: Torsten 2022 年 4 月 10 日
Put "-M" in the matrix of inequality constraints where x_jj is addressed and "+1" where y(j) is addressed.
Then you have set the constraint
-M*x_jj + y_j <= 0
or
M*x_jj - y_j >= 0
This will result in 36 new rows in the constraint matrix A and 36 new zeros in the vector b of the right-hand side.
  3 件のコメント
Torsten
Torsten 2022 年 4 月 10 日
And
for i=1:36
constr(i) = M*x(i,i)-y(i) >= 0
end
does not work ?
Steven Lord
Steven Lord 2022 年 4 月 10 日
So you only want to operate on diagonal elements of x?
x = magic(4)
x = 4×4
16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1
y = [1; 2; 3; 4]
y = 4×1
1 2 3 4
result = 2*diag(x) - y
result = 4×1
31 20 9 -2
n = 3;
spotcheck = [2*x(n, n)-y(n), result(n)] % These should match
check = 1×2
9 9

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeSolver Outputs and Iterative Display についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by