How can you extend a vector with the last value?

2 views (last 30 days)
Hi,
I have two vectors with different dimensions. Vector X is 1x5, the other vector A has the dimension 1x3.
Now I want to plot(X,A), but because of the different dimensions of the vectors this obviously doesn't work.
A solution that would work for me is to take the last value of A and extend the vector to a 1x5 format so that the 4th and 5th value is the same as the last value (in this case the 3rd).
So it should look something like
A = ( 1 A_new = (1
2 ---> 2
3) 3
3
3)
This is only supposed to serve as an example, in my real problem the dimensions are like 1x92000. That's why putting in the values by hand would be very time-consuming.
I would love some help for this question, thanks in advance!
  1 Comment
Bruno Luong
Bruno Luong on 14 Oct 2022
Instead of extend with last value, which is an emperical extension, why not fill with NaN, so that the non-exsiating data is not plotted
X = 1:10;
A = rand(1,5);
A_pad = A;
A_pad(end+1:length(X)) = NaN;
plot(X, A_pad)

Sign in to comment.

Answers (1)

VBBV
VBBV on 14 Oct 2022
Edited: VBBV on 14 Oct 2022
X = rand(1,5)
X = 1×5
0.3283 0.7485 0.9979 0.2897 0.8934
A = rand(1,3)
A = 1×3
0.9032 0.1387 0.2475
Anew = [A repmat(A(end),1,size(X,2)-size(A,2))]
Anew = 1×5
0.9032 0.1387 0.2475 0.2475 0.2475

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!