Need Help for creating constraint with If conditions
조회 수: 3 (최근 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!