Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a MALTAB function:
find(x > [x(1)-1;x(1:n-1)] & x > [x(2:n);x(n)-1]);
How can I unvectorize this? I am not quite sure what it is even testing for!
Ideas?
Something like this:
result = []
for i = 1:n
if i == 1 % special case, since x(-1) does not exist
x_below = x(1) - 1;
else
x_below = x(i - 1);
end
if i == n % special case, since x(n + 1) does not exist
x_above = x(n) - 1;
else
x_above = x(i + 1);
end
if x(i) > x_below && x(i) > x_above
result = [result, i]; %add found index to result
end
end
So as Eric mentioned, it returns the index of all elements in x that are larger then their neighbors. For x(1), a 'fake' lower neighbor of x(1) - 1 is made, so that x(1) is always larger. Index 1 is thus returned if x(1) > x(2). A similar trick is done for x(n).
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have tried to write a function in order to calculate the n-th element of the Fibonacci series:
function [F] = Fibonacci(n)
if n==0
F = 0 ;
elseif n==1
F = 1 ;
else
F = Fibonaccie(n-1) + Fibonacci(n-2) ;
end
end
but as a result I get this:
Undefined function 'Fibonaccie' for input arguments of type 'double'.
Error in Fibonacci (line 14)
F = Fibonaccie(n-1) + Fibonacci(n-2) ;
I don't know where the problem really is.
There is a typo in line 14. You had just written "Fibonaccie" but must have been Fibonacci..
Btw, just note that you dont have to write elseif condition. You can do it with only one else using or statement. However to make your code more robust you should consider n < 0 case:
function F = Fibonacci(n)
if n < 0
F = 0;
elseif n == 0 || n == 1
F = n ;
else
F = Fibonacci(n-1) + Fibonacci(n-2) ;
end
end
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am trying to find the coordinates of all non zero elements in a matrix, in MATLAB. I know that find makes this very easy. The problem is that I need to define the length of my matrix before I can fill it up. I believe this is impossible using find, since it will make its own vector.
So I have to find another way. If I'm looking for 1 element it will be easy as well. Let me use an example. Say I have a matrix M of 500×500, with 60 ones somewhere. I want to find the coordinates. So I can start with:
for i = 1:500
for j = 1:500
if M(i,j) == 1
row = i;
col = j;
end
end
end
But I want more then one point, and I need to define the length of a vector before filling it. So I'll make the gamble that there are less then 100 ones in the matrix:
v = zeros(1,100)
w = zeros(1,100)
And how I essentially want to fill this vector is as follows. Let's say I have already filled N elements of the vector, so the next one will be:
for i = 1:500
for j = 1:500
if M(i,j) == 1 && i ~= v(1) && i ~= v(2) && ... && i ~= v(N) && j ~= v(1) && ... && i ~= v(N)
v(N+1) = i
w(N+1) = j
end
end
end
So I need another for loop containing all this, which will run through the elements of the vectors v and w, and I need some loop that will make sure the right if statement is being used.
Does anyone have any idea of how to do this?
Kind regards,
Jesse
Here's another way, nice & wasteful:
[jj,ii] = meshgrid(1:size(M,2),1:size(M,1));
inds = M~=0;
ii = ii(inds);
jj = jj(inds);
I honestly cannot understand what's wrong with find.
But, if you insist on your own implementation using nested loop, then
v = NaN(100,1);
w = NaN(100,1); % pre-allocate
ii=1;
for row=1:size(M,1)
for col=1:size(M,2)
if M(row,col) == 1
v(ii)=row;
w(ii)=col;
ii=ii+1; %counts howmany elements were found
end
end
end
v(isnan(v))=[]; %discard redundent elements
w(isnan(w))=[];
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
for X = 0: 0.00001 : 100;
M=(((sqrt(2*X)-(1/3))^2)*(2*X-1));
N=(( 10.99743/sqrt(X)-(23/30))^2)*(( 53.75263/X)-1);
if M == N
Y=X;
end
end
this code is for an equation with two sides M&N,,, it must be solved using try and error or Matlab Numerical
why this code is false?
Your problem is (as nkjt points out) with comparing floating point values with ==.
FYI: If your objective is to find for which X, M == N, then you could do it much simpler using for instance fzero:
f = #(X) (((sqrt(2.*X)-(1/3))^2)*(2.*X-1))- ...
(( 10.99743./sqrt(X)-(23/30))^2)*(( 53.75263./X)-1);
y = fsolve(f,1)
y = 6.0304
This gives:
M - N = -1.3743e-05
Which is quite close to zero (as close as you will get).
If you absolutely want try and fail, see if you can decipher this (based on the idea of nkjt):
step = 1;
X = 1:step:100
M=(((sqrt(2*X)-(1/3)).^2).*(2*X-1));
N=(( 10.99743./sqrt(X)-(23/30)).^2).*(( 53.75263./X)-1);
[Y, idx] = min(abs(N-M))
err = 1e-10; % Arbitrary tolerance > 1e-14 (typically)
it = 0;
while Y > err && it < 10
step = step / 10;
it = it + 1;
X = X(idx) + (-step*10:step:step*10);
M=(((sqrt(2*X)-(1/3)).^2).*(2*X-1));
N=(( 10.99743./sqrt(X)-(23/30)).^2).*(( 53.75263./X)-1);
[Y, idx] = min(abs(N-M))
end
The perils of floating point comparison could be one factor. When comparing two floating point numbers, you should not use == but check if the difference between them is less than some tolerance (can use eps or something else, depending on your requirements).
Rather than looping at very small steps across your entire range of possible values, you can vectorise, start with a coarser range of X values and get a much better idea of where your solution probably lies, for example:
X = 0:1:100
M=(((sqrt(2*X)-(1/3)).^2).*(2*X-1));
N=(( 10.99743./sqrt(X)-(23/30)).^2).*(( 53.75263./X)-1);
Y = abs(M-N);
plot(X,Y);
Then find the minimum difference between M and N, reduce your step size and range, and continue until you get to a value for X for which the value of abs(M-N) is below your tolerance (required accuracy of your solution).
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have these two equations and I want to find the values of these two parameters:
9.393e(16) = ((N*K)/(K + 0.0045))*(1 - exp (-(K + 0.0045)*120))
1.376e (17) = ((N*K)/(K + 0.0045))*(1 - exp (-(K + 0.0045)*240))
How can I solve it in matlab or wolfram please
I guess a hand calculator is sufficient for that.
Call:
a = 9.393e(16)
b = 1.376e (17)
Q = (N*K)/(K + 0.0045)
f = exp (-(K + 0.0045)*120) => exp (-(K + 0.0045)*240) = f^2
You have:
a = Q (1 - f)
b = Q (1 - f^2)
so
a/b = (1 - f) / (1 - f^2) = 1 / (1 + f)
thus
f = b/a - 1
You can take the log at both sides and solve for K.
-(K + 0.0045)*120 = log(b/a - 1)
To find N the equation is again just linear.
You can solve simultaneous non-linear equations in MATLAB via FSOLVE or LSQNONLIN. However, this requires the Optimization Toolbox.
See this MathWorks knowledgebase article.
Given the magnitude of the LHS of your equations, I would not be surprised if you see some numerical instability. You might want to do this problem by hand as suggested by Acorbe.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
i want to find the value of C such that standard deviation of A-B*C is minimum using matlab (A & B are vectors) where as C would be a scalar. Any ideas ?
This isn't a programming problem, it's a math problem. You want to find c such that
Var(A - c * B)
is minimized. But
Var(A - c*B) = Var(A) - 2 * c * Cov(A,B) + c^2 * Var(B)
Differentiating and setting to zero
-2 * Cov(A,B) + 2 * c * Var(B) = 0
which implies
c = Cov(A,B) / Var(B)
You can achieve this in Matlab with
M = cov(A, B); # Now M = [varA, covAB; covAB, varB]
covAB = M(1,2);
varB = M(2,2);
c = covAB / varB;
Following #EitanT's advice, try:
Ctry = 1.5; % define trial parameter first
Copt = fminsearch(#(x) sum((A-B*x).^2),Ctry)
edit
Note that in the above I assumed you wanted to minimize the vector norm, but following commentaries it is evident that you want to minimize the std dev of samples in array (A-B*x), in which case try
Ctry = 1.5; % define trial parameter first
Copt = fminsearch(#(x) var(A-B*x),Ctry)
If you are performing columnwise substraction of vector B from A, you can do as follows:
A=rand(900,100); B=randn(900,1); % example
Ctry = 1.5; % define parameter first
B = repmat(B,size(A,2),1);
Copt = fminsearch(#(x) var(A(:)-B*x),Ctry)
Eopt=var(A(:)-B*Copt)
#Luis Mendo suggests
nA = numel(A);
Copt = fminsearch(#(x) sum((A(:)-B*x).^2)-sum(A(:)-B*x)^2/nA,Ctry)
This is a little faster on my system.
Also, as with all minimization problems, it helps if you have a good starting estimate.
Edit
It's worth noting that the two methods find points that differ in the second significant figure, which raises the question which is more accurate. <-- just an issue that can be fixed with optimset