How can you extend a vector with the last value?

2 次查看(过去 30 天)
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 个评论
Bruno Luong
Bruno Luong 2022-10-14
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)

请先登录,再进行评论。

回答(1 个)

VBBV
VBBV 2022-10-14
编辑:VBBV 2022-10-14
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

类别

Help CenterFile Exchange 中查找有关 Multirate Signal Processing 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by