3

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

What should I do?

2
  • 1
    @Guddu huhhuh, this was once a funny slip of the tongue during a presentation. facepalm Commented Jan 19, 2014 at 2:00
  • 1
    This is so duplicated 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. Commented Jan 19, 2014 at 2:18

2 Answers 2

24

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Do I need to declare it first? like A = {}
@FarticlePilter No, you don't.
a = [1 2 3]; a = [A 4];
a = [1 2 3]; a = [a 4];
Hi, I was trying to add elements to the end of array inside a loop, but for some reason it won't work. cols = []; for iu = 1:length(id_unique) [~,col] = find(faces(:,:)== iu); cols(end+1) = col; end Do you see a problem in here?
12

Another way is

A(end+1) = elem; 

which works for both row and column vectors.

2 Comments

Of course, you have to compute "end" first. Good point though.
@herohuyongtao: No, end is the MATLAB keyword.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.