Filter löschen
Filter löschen

how to write this function

1 Ansicht (letzte 30 Tage)
Quynh tran
Quynh tran am 20 Apr. 2017
Bearbeitet: Torsten am 20 Apr. 2017
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 Kommentar
Stephen23
Stephen23 am 20 Apr. 2017
Bearbeitet: Stephen23 am 20 Apr. 2017
@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.

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Torsten
Torsten am 20 Apr. 2017
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 Kommentare
Stephen23
Stephen23 am 20 Apr. 2017
Bearbeitet: Stephen23 am 20 Apr. 2017
"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 am 20 Apr. 2017
Bearbeitet: Torsten am 20 Apr. 2017
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.

Melden Sie sich an, um zu kommentieren.


Stephen23
Stephen23 am 20 Apr. 2017
Bearbeitet: Stephen23 am 20 Apr. 2017
>> idx = A>1 & A<4 & B>1 & B<3 & C>1 & C<4;
>> min(obj(idx))
ans =
0.1
  2 Kommentare
Quynh tran
Quynh tran am 20 Apr. 2017
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 am 20 Apr. 2017
Bearbeitet: Stephen23 am 20 Apr. 2017
@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

Melden Sie sich an, um zu kommentieren.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by