Compare the entire elements of matrix with a number
2 views (last 30 days)
Show older comments
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
1 Comment
Answers (1)
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
on 24 Jun 2019
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.
See Also
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!