フィルターのクリア

Need Help for creating constraint with If conditions

1 回表示 (過去 30 日間)
Taner Cokyasar
Taner Cokyasar 2016 年 7 月 9 日
編集済み: dpb 2016 年 7 月 11 日
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.

採用された回答

dpb
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 件のコメント
Taner Cokyasar
Taner Cokyasar 2016 年 7 月 10 日
編集済み: Taner Cokyasar 2016 年 7 月 10 日
All these are above my recent programming knowledge. I understand you can create multiple ways to solve my problem, but, I can't really understand how you are doing these. I believe, I need to start from the beginning to learn all. I am lost in your codes :(
By the way, the reason is I asked the question in a wrong format. That is experience. Now, I got how I need to ask it :)
dpb
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 ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by