the meaning of length in my loop

2 views (last 30 days)
AMIR AZMI ABD RAHMAN
AMIR AZMI ABD RAHMAN on 23 Dec 2020
Answered: Cris LaPierre on 23 Dec 2020
hii so my lecturer provide us with a code, and he wants us to understand the meaning of each line sadly i couldnt understand what does the length(n) means like why is it equal to 1?
% Function have 4 inputs i.e., n, p1, p2, and p2
% Where n can be a number or row vector function y=padovan(n, p1, p2, p3)
P(1)=p1; P(2)=p2; P(3)=p3;
% Check if n is vector or not
if length(n) == 1
for i=4:n P(i)=P(i-2)+P(i-3);
end
else for i=4:length(n) P(i)=P(i-2)+P(i-3);
end
end
y=P(n);

Answers (2)

Star Strider
Star Strider on 23 Dec 2020
The length function returns the largest dimension of its argument.
The line:
if length(n) == 1
It is not setting it equal to 1, it is testing logically if it is equal to 1.
Since this appears to be a homework assignment, I will let you explore the documentation in order to understand how the rest of the code works.

Cris LaPierre
Cris LaPierre on 23 Dec 2020
Every function in MATLAB is documented. There, you can find a description, examples, and more. Check out the documentation for length here.

Categories

Find more on Software Development Tools in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!