1

I wish to append my elem to the end of an array A.

What should I do?

Sibbs Gambling
  • 19,274
  • 42
  • 103
  • 174
  • 1
    @Guddu huhhuh, this was once a funny slip of the tongue during a presentation. *facepalm* – Sibbs Gambling Jan 19 '14 at 02:00
  • 1
    This is [so duplicated](https://www.google.com/search?client=safari&rls=en&q=matlab+append+stackoverflow&ie=UTF-8&oe=UTF-8) that I don't know where to begin. Do you have a more specific question because I'm sure that you must have searched first. – horchler Jan 19 '14 at 02:18

2 Answers2

22

Use the following

A = [A elem] % for row array

or

A = [A; elem] % for col array

Edit: Another simpler way is (as @BenVoigt suggested) to use end keyword

A(end+1) = elem;

which works for both row and column vectors.

herohuyongtao
  • 49,413
  • 29
  • 133
  • 174
12

Another way is

A(end+1) = elem;

which works for both row and column vectors.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720