필터 지우기
필터 지우기

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 CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by