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!