Need Help for creating constraint with If conditions
1 次查看(过去 30 天)
显示 更早的评论
I have 'Z(im)', 't(m)', and f(i) vectors as following (left side is parameter indices=Zim):
im Zim t(m) f(i)
11 0 12 6
12 0 11 6
13 1 10 6
21 0 12 3
22 1 11 3
23 0 10 3
31 0 12 5
32 1 11 5
33 0 10 5
and an equality constraint as following:
Z(im)*Y(im)+S(im)=t(m)-Z(im)*f(i) for all i and m, *IF Zim=1*
I need to find Rhs of equalities for values where Zim=1.
For instance, we know Z13, Z22, and Z32 are all equal to 1. Thus I want to create a 3x1 (mx1) column vector for those right hand side values as following:
b=[4; 8; 6]
I believe, I need to use for loop and if command. But, I can't figure out a way to construct a loop for this question.
Thanks for help.
0 个评论
采纳的回答
dpb
2016-7-9
No loops needed...given your above array as m,
>> ix=m(:,2)==1;
>> b=m(ix,3)-m(ix,4)
b =
4
8
6
>>
8 个评论
dpb
2016-7-10
编辑:dpb
2016-7-11
"For Spos and Ypos, you could say: Spos = i*m; Ypos = 2*i*m; because i=3 and m=3"
No. The general expression for the row location from 1 thru 9 for the 3x3 array locations is (m-1)+3(i-1)+1.
>> a=reshape(1:9,3,3) % a 3x3 array in memory order
a =
1 4 7
2 5 8
3 6 9
>> [i j]=ind2sub(size(a),1:9); % the subscripts from linear order
>> [i.' j.']
ans =
1 1
2 1
3 1
1 2
2 2
3 2
1 3
2 3
3 3
>> for i=1:3, % from explicit expression
for m=1:3,
disp([m i (m-1)+3*(i-1)+1]),
end,end
1 1 1
2 1 2
3 1 3
1 2 4
2 2 5
3 2 6
1 3 7
2 3 8
3 3 9
>>
The locations you're wanting in the 18-column array are simply two 9-column array locations placed one to the right of the other. Since each has the same i,m values, they're simply offset by 10 positions from each other; no reason to compute but the left one and then shift it to the right to compute the other.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!