Question regarding a condition difference.
Mostrar comentarios más antiguos
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 comentarios
Gimpy
el 14 de Ag. de 2012
Albert Yam
el 14 de Ag. de 2012
What have you tried?
Azzi Abdelmalek
el 14 de Ag. de 2012
what about A=c? your conditions are > and <
Respuestas (5)
Azzi Abdelmalek
el 14 de Ag. de 2012
Editada: Azzi Abdelmalek
el 15 de Ag. de 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 comentarios
Gimpy
el 14 de Ag. de 2012
Gimpy
el 14 de Ag. de 2012
Azzi Abdelmalek
el 14 de Ag. de 2012
Editada: Azzi Abdelmalek
el 14 de Ag. de 2012
if i have understood your question that will be
c= min(A)
4 comentarios
Gimpy
el 15 de Ag. de 2012
Azzi Abdelmalek
el 15 de Ag. de 2012
Editada: Azzi Abdelmalek
el 15 de Ag. de 2012
instead
result(find(A-c>=0))
use
result(arrayfun(@(x) x<0,A-c))=0
Azzi Abdelmalek
el 15 de Ag. de 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
el 15 de Ag. de 2012
0 votos
Azzi Abdelmalek
el 15 de Ag. de 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 comentario
Gimpy
el 15 de Ag. de 2012
0 votos
Categorías
Más información sobre Operating on Diagonal Matrices en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!