フィルターのクリア

how to write this function

1 回表示 (過去 30 日間)
Quynh tran
Quynh tran 2017 年 4 月 20 日
編集済み: Torsten 2017 年 4 月 20 日
I have this problem:
% A B C obj
P =[1 4 4 0.1
2 6 6 0.1
4 7 7 0.4
5 8 2 0.3
7 9 3 0.2
8 0 9 0.5
3 2 2 0.1
4 1 5 0.2
6 6 4 0.3
7 7 1 0.4
8 8 3 0.5]
A=P(:,1);
B=P(:,2);
C=P(:,3);
obj=P(:,4);
n=size(P,1);
I want to write code to find min (obj) subject to: 1<A<4;1<B<3; 1<C<4 and show the result on the first row of P. I tried to write it but alway give me a wrong results. Could you help me to solve it? Thanks.
  1 件のコメント
Stephen23
Stephen23 2017 年 4 月 20 日
編集済み: Stephen23 2017 年 4 月 20 日
@Quynh tran: can you please explain your request "...show the result on the first row of P": How do you want the result to be shown in P ? Can you please give an example of this.

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

回答 (2 件)

Torsten
Torsten 2017 年 4 月 20 日
minimum = inf;
index = -1;
for k=1:numel(A)
if A(k)>1 && A(k)<4 && B(k)>1 && B(k)<3 && C(k)>1 && C(k)<4 && obj(k)<minimum
index = k;
minimum = obj(k);
end
end
index
minimum
Best wishes
Torsten.
  4 件のコメント
Stephen23
Stephen23 2017 年 4 月 20 日
編集済み: Stephen23 2017 年 4 月 20 日
"I like for-loops because they clearly show what you are doing"
Just to clarify: you are saying that this is too complicated to understand?:
idx = A>1 & A<4 & B>1 & B<3 & C>1 & C<4;
min(obj(idx))
Especially when compared to this:
minimum = inf;
index = -1;
for k=1:numel(A)
if A(k)>1 && A(k)<4 && B(k)>1 && B(k)<3 && C(k)>1 && C(k)<4 && obj(k)<minimum
index = k;
minimum = obj(k);
end
end
I just wanted to understand how three temporary variables, a for loop, an if, and and several "magic numbers" are easier to understand than one simple logical index (as we all know, logical indexing is so fundamental to using MATLAB efficiently, as it is usually the fastest way to access data in an array).
Do you have a programming background?
Torsten
Torsten 2017 年 4 月 20 日
編集済み: Torsten 2017 年 4 月 20 日
Do you have a programming background?
Well, yes, it's the classical background you mentioned above (Fortran & C).
Maybe that's why MATLAB as a high-level language often appears quite "unnatural" to me. But things might be different if one grew up with MATLAB.
Best wishes
Torsten.

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


Stephen23
Stephen23 2017 年 4 月 20 日
編集済み: Stephen23 2017 年 4 月 20 日
>> idx = A>1 & A<4 & B>1 & B<3 & C>1 & C<4;
>> min(obj(idx))
ans =
0.1
  2 件のコメント
Quynh tran
Quynh tran 2017 年 4 月 20 日
Sorry, could you show the result of A,B,C because we can see have many value of A,b,c get min(obj)=0.1. Thanks
Stephen23
Stephen23 2017 年 4 月 20 日
編集済み: Stephen23 2017 年 4 月 20 日
@Qunyh tran: your request is not clear: if you want to see the indices for each column then this is easy:
>> idA = A>1 & A<4
idA =
0
1
0
0
0
0
1
0
0
0
0
>> min(obj(idA))
ans = 0.10000
and of course you can do the same for B and C. It is easy to combine them as well (just imagine how difficult this would be with a for loop!)
>> min(obj(idA&idB&idC))
ans = 0.10000

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

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by