필터 지우기
필터 지우기

Question regarding a condition difference.

조회 수: 1 (최근 30일)
Gimpy
Gimpy 2012년 8월 14일
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
Albert Yam
Albert Yam 2012년 8월 14일
What have you tried?
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
Azzi Abdelmalek
Azzi Abdelmalek 2012년 8월 15일
it's not obvious
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에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by