Suicide Bunny
Suicide Bunny

Reputation: 938

How to keep chopping the tail of an array (leading to an empty set)

Say A=[1,3];. I want to keep chopping the tail of A, except that MALAB does not have a "A.pop()"

I tried to write a code like

for i=m:-1:1;

A=A(1:i-1);  
end

but MATLAB says "Subscript indices must either be real positive integers or logicals."

Upvotes: 0

Views: 229

Answers (2)

CodeMonkey
CodeMonkey

Reputation: 1835

You can also try this I guess:

if length(A) > 1
    A(end:end) = []
else
    A = []
end

Upvotes: 1

CodeMonkey
CodeMonkey

Reputation: 1835

Like this:

if length(A) > 1
    A = A(1:length(A)-1)
else
    A = []
end

Upvotes: 2

Related Questions