Question regarding a condition difference.

2 次查看(过去 30 天)
Hi, I'm new on this forum, hope I will be doing good.
---------------------------------------------------------------------
Here's my question: I have 3 matrix, for this example let's say 3 vectors
A, B and C (is a scalar)
I want to do the following operation :
for each element in vector A, if the element > C then A-B, else the element =0.
---------------------------------------------------------------------------
The only way I know is the do a loop for echa element and check the condition. I don't like this option since A and B are going tho be 25200 rowa and 15 column. I assume it's going to take tome much time this way.
Any would be appreciate.
regards
Gimpy
  3 个评论
Azzi Abdelmalek
Azzi Abdelmalek 2012-8-14
what about A=c? your conditions are > and <

请先登录,再进行评论。

回答(5 个)

Azzi Abdelmalek
Azzi Abdelmalek 2012-8-14
编辑:Azzi Abdelmalek 2012-8-15
here an example
clear
a1=[88;57;42;100];
a2=[98;87;32;80];
A=[a1 a2];
b1=[78;5;32;106];
b2=[88;87;35;88];
B=[b1 b2];
c=[ones(4,1)*80.5 ones(4,1)*89.27];
result=A-B; result=A-B;result(arrayfun(@(x) x<0,A-c))=0
% the result is result =
10 10
0 0
0 0
-6 0
  3 个评论
Gimpy
Gimpy 2012-8-14
To be more precise : if A>=C then A-B, else A-B=0 (result wanted in a mtrix for each rows).
Then I want to find wich "C" is going to maximise the previous condition.
Thanks again
Regards
Gimpy
Azzi Abdelmalek
Azzi Abdelmalek 2012-8-14
编辑:Azzi Abdelmalek 2012-8-14
if i have understood your question that will be
c= min(A)

请先登录,再进行评论。


Gimpy
Gimpy 2012-8-15
编辑:Gimpy 2012-8-15
first let's get back to my initial question. Here's what I wan't to do exactly:
A=rand(10,2)*100;
B=rand(10,2)*100;
c1=ones(10,1)*80.5;
c2=ones(10,1)*89.27;
c=[c1 c2];
result=A-B;
result(find(A-c>=0))
the only problem I have with this is that the variable"result" is an "nx1", I would like to have the results as nx2 (maybe with 0 or something). In this case the variable "results" would be a 10x2 (i guess with zeros where the condition (A-c>=0 is not met.
  4 个评论
Gimpy
Gimpy 2012-8-15
编辑:Gimpy 2012-8-15
not exactly(it don't seems to be working some conditions are respected but not report in"R"). Here's what I'm looking exactly:
a1=[88;57;42;100];
a2=[98;87;32;80];
A=[a1 a2];
b1=[78;5;32;106];
b2=[88;87;35;88];
B=[b1 b2];
c=[ones(4,1)*80.5 ones(4,1)*89.27];
result=A-B; result=A-B;result(find(A-c>=0))
ans =
10
-6
10
*** the result is good but I want it this way:
10 10
0 0
0 0
-6 0
Azzi Abdelmalek
Azzi Abdelmalek 2012-8-15
i have already updated a code
clear
a1=[88;57;42;100];
a2=[98;87;32;80];
A=[a1 a2];
b1=[78;5;32;106];
b2=[88;87;35;88];
B=[b1 b2];
c=[ones(4,1)*80.5 ones(4,1)*89.27];
result=A-B; result=A-B;result(arrayfun(@(x) x<0,A-c))=0
% the result is result =
10 10
0 0
0 0
-6 0

请先登录,再进行评论。


Gimpy
Gimpy 2012-8-15
Yes that's exactly what I'm looking for.
The seceond step(and the last one)I want to do the sum of the variable"results" and get the maximum.In this case the result would be in the form of: 1x2
So clearly I want to find the "c" that maximise "result2":
result2=sum(result)
(in the previous example the "c" were 80.5 and 89.27
So the point is when we do
result=A-B;
result(~(A-c>=0)) = 0
Some "A-B" yield a negative results so I want to find the c that
optimize (thus skip the A-B that are negative.
Let's set the "c" as c1 and c2
the final optization is going to be in the following dimesnion:
c=[ones(4,1)*c1 ones(4,1)*c2];
Thank you in advance
Gimpy
  2 个评论
Gimpy
Gimpy 2012-8-15
ok here's my example again: A=[45;37;32;50]
B=[17;100;200;10]
c=??(the value we look for)
The condition to test: A=[45;37;32;50]
B=[17;100;200;10]
c=? (the value we are looking for)
the condition to test: A >= C then A-B, else A-B=0 in my example the C we look for is something between 37.01 and 50 because.
In excel for example to be more specific: cells A2= 45,A3=37,A4=32,A5=50
B2=17, B3=100, B4=200, B5=10
C2:C5=37.01 (in this case i know the results by trial and error).
The condition to test is:
=IF(A2>=C2,(A2-B2),0)= 28 =IF(A3>=C3,(A3-B3),0)=0 =IF(A4>=C4,(A4-B4),0)=0 =IF(A5>=C5,(A5-B5),0)=40
Total: 68
Any value between 37.01 and 50 would give the save result. A way to find the lower and uper bound would be appreciate of only one...
Thanks again for your patience and help.

请先登录,再进行评论。


Azzi Abdelmalek
Azzi Abdelmalek 2012-8-15
if you give the interval, we can use a while loop
A=[45;37;32;50]
B=[17;100;200;10]
c=37.01; som=[];
while c<50
result=A-B;
result=A-B;result(arrayfun(@(x) x<0,A-c))=0
som=[som ;[sum(result) c]];
c=c+0.1
end
somax=max(som(:,1))
c_result=som(find(som==somax),2)
lower_c=min(c_result)
upper_c=max(c_result)
  1 个评论
Gimpy
Gimpy 2012-8-15
编辑:Gimpy 2012-8-15
We don't know the interval. It s the result we are looking for. Again the equation to maximize is:
For each element in "A"(let's call them ai) if this ai is > to c then
A-B, else 0
Need to find the C that maximize result2
result=A-B;
result(~(A-c>=0)) = 0
result2 =sum(result)

请先登录,再进行评论。


Gimpy
Gimpy 2012-8-15
Here’s my problem with more explanation again:
Matrix A,B,C :22 500 x18
Matrix c : each colum are scalar
Exemple :[50;50;50;50 72.25;72.25;72.25;72.25]
… Here’s what I want to do :
For each elements in A, lets call them ai, if ai >ci then
ai - bi
else
ai - bi =0
Here’s what I do in matlab(for simplification I do a 4x1)
a1=[88;57;42;100];
a2=[98;87;32;80];
A=[a1 a2];
b1=[78;5;32;106];
b2=[88;87;35;88];
B=[b1 b2];
c=[ones(4,1)*80.5 ones(4,1)*89.27];
result=A-B;
result(~(A-c>=0)) = 0
result2=sum(result)
****Now here’s the point : I want to find the matrix c that’s going to maximise :
result2=sum(result).
The results should be an interval
In the revious example I’am expecting MATLAB to give something like:
resultat2= 68
C_lower= 37
C_upper=45
Or at least one one of the 2 results (37 or 45).
I found this result by calculating result2=sum(result) by trial and error until
I get the maximum value(result2=68).
Please keep I mind that I want to work with 22500x18

类别

Help CenterFile Exchange 中查找有关 Quadratic Programming and Cone Programming 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by