Question regarding a condition difference.
显示 更早的评论
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 个评论
Gimpy
2012-8-14
Albert Yam
2012-8-14
What have you tried?
Azzi Abdelmalek
2012-8-14
what about A=c? your conditions are > and <
回答(5 个)
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
2012-8-14
Gimpy
2012-8-14
Azzi Abdelmalek
2012-8-14
编辑:Azzi Abdelmalek
2012-8-14
if i have understood your question that will be
c= min(A)
4 个评论
Gimpy
2012-8-15
Azzi Abdelmalek
2012-8-15
编辑:Azzi Abdelmalek
2012-8-15
instead
result(find(A-c>=0))
use
result(arrayfun(@(x) x<0,A-c))=0
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
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)
类别
在 帮助中心 和 File Exchange 中查找有关 Operating on Diagonal Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!