Compare the entire elements of matrix with a number

2 views (last 30 days)
Hi everyone, please guide me, I wanna to build a function that compares every elements in a matrix say A with a-number say H for example; For n=1:1000 e=0.005; H= (n-1)*e; A=[1 2 3; 3 4 5; 7 10 15]; If A>=H Bounce = 0 else Bounce = 1 end end

Answers (1)

Sulaymon Eshkabilov
Sulaymon Eshkabilov on 23 Jun 2019
Hi,
Here is the one of the possible solutions:
e=0.005;
n=1:1000;
H=(n-1)*e;
A=[1 2 3; 3 4 5; 7 10 15];
for ii=1:1000;
if A>=H(ii)
Bounce =0
else
Bounce=1
end
end
  4 Comments
madhan ravi
madhan ravi on 24 Jun 2019
Sulaymon Eshkabilov's answers moved here for consistency:
There are some issues in your initial given data that need to be fixed. (1) Your set initial values of Xzero and Yzero are meant to make the computed values of X and Y to cross each other. (2) L is very large value and thus, X will go to inf when D<H(ii). Thus, L has to be adjusted. (3) The range of "n" has to be selected accordingly.
When you fix these two issues, this code should work:
clear variables
randn('state', 100)
mu = -2; mu2 = 2; sigma=1; sigma2=1;
Xzero = 50; Yzero = 10;
Delta = 0.005;
p = 10; N = 2^p; T = 1; dt = 1/N; n=1:1000;
dW = sqrt(dt)*randn(1,N); % Brownian increments
W = cumsum(dW); % discretized Brownian path
Alpha = mu*([dt:dt:T])+sigma*W;
Betta = mu2*([dt:dt:T])+sigma2*W;
A = log(Xzero)+ Alpha; B = log(Yzero)+ Betta;
H=(-2*(n-1)*Delta);
D = (A-B); L = sum((n-1)*Delta);
X=zeros(size(n)); Y=X;
for ii=n;
if D>=H(ii)
Bounce =0;
else
Bounce=L;
end
X(ii) = Xzero*(exp(Alpha(ii)+Bounce));
Y(ii) = Yzero*(exp(Betta(ii)-Bounce));
end
Good luck
Note that the size of "n" has to match with the size of 0:dt:T, if not then the plot function will produce the error that you encountered already. Or vice-versa, the size of dt has to be selected with respect to n.
anas khalaf
anas khalaf on 25 Jun 2019
Thank you so much dear Sir
I already did that, with all these edits the same Propblem still arising, the processes still cross each other the Bouncing doesint work on the whole interval !!!!
clear variables
randn('state', 100)
mu = -2; mu2 = 2; sigma=1; sigma2=1;
Xzero = 50; Yzero =10;
Delta = 0.005;
p = 10; N = 2^p; T = 1; dt = 1/N; n=1:N;
% rnd = randn(1,N);
dW = sqrt(dt)*randn(1,N); % Brownian increments
% dW = sqrt(dt)*rnd; % Brownian increments
W = cumsum(dW); % discretized Brownian path
Alpha = mu*([dt:dt:T])+sigma*W;
M = 2^p; S = 1; ds = 1/M;
dW2 = sqrt(ds)*randn(1,M); % Brownian increments2
% dW2 = sqrt(ds)*rnd; % Brownian increments2
W2 = cumsum(dW2);
Betta = mu2*([ds:ds:S])+sigma2*W2;
A = log(Xzero)+ Alpha; B = log(Yzero)+ Betta;
H=(-2*(n-1)*Delta);
D = (A-B); L =(n-1)*Delta;
X=zeros(size(n)); Y=X;
for ii=n
% L(ii) =sum(n-1)*Delta;
%L(ii) =(ii-1)*Delta;
if D(ii)>=H(ii)
Bounce =0;
else
Bounce=L(ii);
end
X(ii) = Xzero*(exp(Alpha(ii)+Bounce));
Y(ii) = Yzero*(exp(Betta(ii)-Bounce));
end
plot([0:dt:T],[Xzero,X],'b-'); hold on
plot([0:ds:S],[Yzero,Y],'r-'); hold off
xlabel('Time')
ylabel('position')
untitled2.jpg

Sign in to comment.

Categories

Find more on Operators and Elementary Operations 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!