# how to write this function

4 visualizaciones (últimos 30 días)
Quynh tran el 20 de Abr. de 2017
Editada: Torsten el 20 de Abr. de 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 comentarioMostrar -1 comentarios más antiguosOcultar -1 comentarios más antiguos
Stephen23 el 20 de Abr. de 2017
Editada: Stephen23 el 20 de Abr. de 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.

Iniciar sesión para comentar.

### Respuestas (2)

Torsten el 20 de Abr. de 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 comentariosMostrar 2 comentarios más antiguosOcultar 2 comentarios más antiguos
Stephen23 el 20 de Abr. de 2017
Editada: Stephen23 el 20 de Abr. de 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 el 20 de Abr. de 2017
Editada: Torsten el 20 de Abr. de 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.

Iniciar sesión para comentar.

Stephen23 el 20 de Abr. de 2017
Editada: Stephen23 el 20 de Abr. de 2017
>> idx = A>1 & A<4 & B>1 & B<3 & C>1 & C<4;
>> min(obj(idx))
ans =
0.1
##### 2 comentariosMostrar NingunoOcultar Ninguno
Quynh tran el 20 de Abr. de 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 el 20 de Abr. de 2017
Editada: Stephen23 el 20 de Abr. de 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

Iniciar sesión para comentar.

### Categorías

Más información sobre Logical en Help Center y File Exchange.

### Community Treasure Hunt

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

Start Hunting!

Translated by