Question regarding a condition difference.
Show older comments
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 Comments
Gimpy
on 14 Aug 2012
Albert Yam
on 14 Aug 2012
What have you tried?
Azzi Abdelmalek
on 14 Aug 2012
what about A=c? your conditions are > and <
Answers (5)
Azzi Abdelmalek
on 14 Aug 2012
Edited: Azzi Abdelmalek
on 15 Aug 2012
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 Comments
Gimpy
on 14 Aug 2012
Gimpy
on 14 Aug 2012
Azzi Abdelmalek
on 14 Aug 2012
Edited: Azzi Abdelmalek
on 14 Aug 2012
if i have understood your question that will be
c= min(A)
4 Comments
Gimpy
on 15 Aug 2012
Azzi Abdelmalek
on 15 Aug 2012
Edited: Azzi Abdelmalek
on 15 Aug 2012
instead
result(find(A-c>=0))
use
result(arrayfun(@(x) x<0,A-c))=0
Azzi Abdelmalek
on 15 Aug 2012
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
on 15 Aug 2012
0 votes
Azzi Abdelmalek
on 15 Aug 2012
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 Comment
Gimpy
on 15 Aug 2012
0 votes
Categories
Find more on Operating on Diagonal Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!