Effacer les filtres
Effacer les filtres

how to write this function

1 vue (au cours des 30 derniers jours)
Quynh tran
Quynh tran le 20 Avr 2017
Modifié(e) : Torsten le 20 Avr 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 commentaire
Stephen23
Stephen23 le 20 Avr 2017
Modifié(e) : Stephen23 le 20 Avr 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.

Connectez-vous pour commenter.

Réponses (2)

Torsten
Torsten le 20 Avr 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 commentaires
Stephen23
Stephen23 le 20 Avr 2017
Modifié(e) : Stephen23 le 20 Avr 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 le 20 Avr 2017
Modifié(e) : Torsten le 20 Avr 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.

Connectez-vous pour commenter.


Stephen23
Stephen23 le 20 Avr 2017
Modifié(e) : Stephen23 le 20 Avr 2017
>> idx = A>1 & A<4 & B>1 & B<3 & C>1 & C<4;
>> min(obj(idx))
ans =
0.1
  2 commentaires
Quynh tran
Quynh tran le 20 Avr 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 le 20 Avr 2017
Modifié(e) : Stephen23 le 20 Avr 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

Connectez-vous pour commenter.

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by