MATLAB Answers

Target just the last added entries to a matrix

1 view (last 30 days)
Vance Blake
Vance Blake on 30 Aug 2019
Commented: Vance Blake on 1 Sep 2019
Hey, I am working with matrices and I wanted to know if there is a standard way/code to target just the last added entries to a matrix. I used the general syntax for appending data points to a matrix (A= [A;B]) and I wanted to know if there was an analogous general method for targetting the last added entries into a matrix. For example, I append 4 sets of coordinates to a matrix and now I want to call just those 4 sets from the matrix and use them for next step in my code. Any help is greatly appreciated.
  4 Comments
Vance Blake
Vance Blake on 30 Aug 2019
Yeah I know that growing dynamically is slow (matlab kindly reminds me every time i run my code) but unfortunately I don't know how big the matrix will be beforehand. But Im not appending the sets of coordinates to the matrix during the loop but after it has been completed.
@Guillaime does your solution only work with a loop ??

Sign in to comment.

Accepted Answer

Guillaume
Guillaume on 30 Aug 2019
As commented:
A(end-n:end, :)
will give you the last n+1 rows of the (2D) matrix.
does your solution only work with a loop
Why do you think that? There's no loop involved in that line. It's basic matrix indexing using the end keyword.
  3 Comments
Vance Blake
Vance Blake on 1 Sep 2019
My bad didnt notice the link but thanks for the help.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by