Info

This question is closed. Reopen it to edit or answer.

Why d(i) is getting stored in a row vector? Shouldn't it be a single value rather than a vector!?

1 view (last 30 days)
'x' and 'y' are column input vectors (Something like 20x1 double sorta thing).
k = input('Number of elements = ')
if k < 4
disp Invalid
elseif k >= 4 && k <= length(x)
for i = 1:1:k
% z = x(i);
% q = y(i);
d(i) = ((x(i+1) - x(i))^2 + (y(i+1) - y(i))^2)^0.5
% disp (d(i))
% for i = k
% a = sum(d(1:i))/i
% end
end
for i = 1:i
msd = (sum(d(1:i)))/i
% disp (msd)
end
elseif k > length(x)
disp Invalid
end

Answers (1)

Sai Sri Pathuri
Sai Sri Pathuri on 31 Oct 2019
Hi,
In the code, you have defined the variable d to be a vector d(i) which increases its size in each iteration of loop from 1 to k. So, it is a row vector with index j containing the value of expression at i = j.
If you expect the variable d to be a scalar which contains the value of expression
((x(i+1) - x(i))^2 + (y(i+1) - y(i))^2)^0.5
at i = k, there is no need of loop. You can just use
d = ((x(k+1) - x(k))^2 + (y(k+1) - y(k))^2)^0.5

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!