The expression to the left of the equals sign is not a valid target for an assignment Matlab - matlab

Trying to write a loop that checks if one norm is bigger than the other.
for i = 1:360;
V1 = X(:, i);
N1 = norm(V1);
V2 = X(:, (i+1));
N2 = norm(V2);
N1 = highNorm;
if (N2 >= N1)
(P = N2)
end
However, I'm getting an error associated with P = N2:
Error: The expression to the left of the equals
sign is not a valid target for an assignment.
How do I fix this?

Get rid of the brackets around P=N2

Related

Bellman-Ford Algorithm Short predecessor Array

Having this Algorithm of Bellman-Ford with a slight change:
In line 7, instead of the inequality sign, we put >= so it becomes:
d[v] >= d[u] + w(u,v)
Now I know for a fact that it will not change the distances array and it will give me a correct answer.
But how will it affect my predecessor array on line 9? will it still give a correct answer?
If you have non positive edges it may not be a tree, for example for the following graph and starting from n1:
In the first pass:
by visiting e1: d[n2] = d[n1] + w[e1] = 1 & p[n2] = n1
by visiting e2: d[n3] = d[n2] + w[e2] = 1 & p[n3] = n2
by visiting e3: d[n4] = d[n3] + w[e3] = 1 & p[n4] = n3
by visiting e4: d[n2] = d[n4] + w[e4] = 1 & p[n2] = n4
and in all remaining passes, this will repeat. so at the end if you iterate over predecessor for example for n2 your will get: n2 <- n4 <- n3 <- n2 <- n4 <- ...
But I think if you don't have non-negative edges it works fine.

Error using /; matrix dimensions must agree

A1 = 0.85;%in^2
L = 2.0; %in
x = -2:0.5:2; %in
V1 = 300; %ft/s
for i=1 : length(x)
A = A1*cosh(x/L);
V = (V1 * A1) / A;
end
I know it's dumb but this is giving me this error and I can't figure out why
Error using /
Matrix dimensions must agree.
Error in Lab2 (line 11)
V = (V1 * A1) / A;
The problem is that you are performing a non element-wise division between a scalar (the result of V1 * A1) and a vector (A). This should fix your problem (for more information, refer to this page of the official documentation):
for i = 1:length(x)
A = A1 * cosh(x / L);
V = (V1 * A1) ./ A;
end
On a side note, it's not clear to me what you are attempting to do in your for loop. You create a vector x but you never really use it's values; only it's length is used in order to set the upper limit of the iteration. On the top of that, since outer scope variables never change, every single iteration produces the same A and V values. You could remove the iteration and still be able to obtain the same result with a single computation, because the inner code is already vectorized on x.

Finding smallest value for parameterised answer that satisfies condition

I want to find the smallest integer l that satisfies l^2 >= x, and mod(l,2)=0.
In the following example x=75, and hence l=10, since the previous even number doesn't fulfil the inequation: 8^2 <= 75 <= 10^2
I have tried this (ignoring the even-number requirement, which I can't to work):
syms l integer
eqn1 = l^2 >= 75;
% eqn2 = mod(l,2) == 0;
[sol_l, param, cond] = solve(eqn1, l, 'ReturnConditions', true);
But this does not give me anything helpful directly:
sol_l =
k
param =
k
cond =
(75^(1/2) <= k | k <= -75^(1/2)) & in(k, 'integer')
I would like to evaluate the conditions on the parameter and find the smallest value that satisfies the conditions.
Also, I would like to enforce the mod(l,2)=0 condition somehow, but I don't seem to get that work.
Using the solve for this task is like using a cannon to kill a mosquito. Actually, the answer of Lidia Parrilla is good and fast, although it can be simplified as follows:
l = ceil(sqrt(x));
if (mod(x,2) ~= 0)
l = l + 1;
end
% if x = 75, then l = 10
But I would like to point out something that no one else noticed. The condition provided by the solve function for l^2 >= 75 is:
75^(1/2) <= k | k <= -75^(1/2)
and it's absolutely correct. Since l is being raised to the power of 2, and since a negative number raised to the power of 2 produces a positive number, the equation will always have two distinct solutions: a negative one and a positive one.
For x = 75, the solutions will be l = 10 and l = -10. So, if you want to find the smallest number (and a negative number is always smaller than a positive one), the right solution will be:
l = ceil(sqrt(x));
if (mod(x,2) ~= 0)
l = l + 1;
end
l = l * -1;
If you want to return both solutions, the result will be:
l_pos = ceil(sqrt(x));
if (mod(x,2) ~= 0)
l_pos = l_pos + 1;
end
l_neg = l_pos * -1;
l = [l_neg l_pos];
I guess the easiest solution without employing the inequality and solve function would be to find the exact solution to your equation l^2 >= x, and then finding the next even integer. The code would look like this:
x = 75;
y = ceil(sqrt(x)); %Ceil finds the next bigger integer
if(~mod(y,2)) %If it's even, we got the solution
sol = y;
else %If not, get the next integer
sol = y+1;
end
The previous code gives the correct solution to the provided example (x = 75; sol = 10)

How to avoid nested for loops in Matlab for better cpu time

I should calculate this formula for large value of p, so 4 nested loops made my code very slow and inapplicable. I will so thankful if anyone can help me for better implementation with use of sum and other suitable matlab commands!
K(i,j)=sum(sum(a(m)*b(n)*A(i,j,m,n),m=1:p),n=1:p);
i,j,m,n ->1:p
and A is 4D Matrix and a,b are vector.
Thank.
I can get rid of 2 of the for loops. Perhaps one of the MATLAB wizards on this site can do better.
p = 3;
A = rand(p, p, p, p)
a = rand(p, 1)
b = rand(p, 1)
% I think your original code does something like this.
K1 = zeros(p, p);
for n = 1: p
for m = 1: p
for j = 1: p
for i = 1: p
K1(i, j) = K1(i, j) + a(m) * b(n) * A(i, j, m, n);
end
end
end
end
K1
% This gives the same result, with half the loops.
K2 = zeros(p, p);
for n = 1: p
for m = 1: p
K2 = K2 + a(m) * b(n) * A(:,:,m,n);
end
end
K2
% Verify that the two answers are the same.
all(K1(:) == K2(:))

Cannot understand this MATLAB syntax?

for i=0:255
m(i+1)=sum((0:i)'.*p(1:i+1)); end
What is happening can anyone explain. p is an array of size 256 elements same as m.
p = (0:255)';
m = zeros(1,256);
for i=0:255
m(i+1)=sum((0:i)'.*p(1:i+1));
end
m[i+1] contains the scalar product of [0,1,2,..,i] with (p[1],...,p[i+1])
You can write it as :
p = (0:255);
m = zeros(1,256);
for i=0:255
m(i+1)=sum((0:i).*p(1:i+1));
end
Or:
p = (0:255);
m = zeros(1,256);
for i=0:255
m(i+1)=(0:i)*p(1:i+1)';
end
In case you don't recall, that is the definition of scalar product
Whatever the p is, you can calculate m by:
dm = (0 : length(p) - 1)' .* p(:); % process as column vector
m = cumsum(dm);
Hint: write the formula for m[n], then for m[n+1], then subtract to get the formula:
m[n+1] - m[n] = (n - 1) * p[n]
and this is dm.