필터 지우기
필터 지우기

Extending a vectors length with zeros

조회 수: 35 (최근 30일)
Chameleon17
Chameleon17 2015년 10월 13일
편집: Tongyao Pu 2019년 10월 17일
Good Afternoon,
I'm trying to extend the length of a vector, say it is 1x3 double. and I want it to be the same length as another vector which is say 1x8 double. I would like to pad it out with zeros but I do not have the image processor application. Is there a simple way to do this?
Thanks you for any help and time you have to spare!

답변 (2개)

Jan
Jan 2015년 10월 13일
x = rand(1, 3);
y = rand(1, 8);
x(length(y)) = 0;
This fills all elements from length(x)+1 to length(y) with zeros.
  댓글 수: 1
Tongyao Pu
Tongyao Pu 2019년 10월 17일
편집: Tongyao Pu 2019년 10월 17일
Ed: I tried again and it works. I think the concept of your code is a bit different.
x(length(y)) = 0;
This only assigns the last number to 0. But because it extended the length of x, matlab assign all the other positions to 0.
For example, if I try;
X(length(y)) = NaN;
It only assigns the last number to NaN. If I want to extend the length of x with NaNs, I need to write:
x(length(x)+1:length(y)) = NaN;
Anyway, thank you for your answer, it inspires me a lot!
Hi, that’s a brilliant solution, but is there a way to store it in a new variable? I copied the variable again
x2 = x;
but was wondering if there is a more direct way?
Also, instead of assign it to 0, can I assign it to NaN (I tried the same code but it still turns out to be 0s)?
Thanks

댓글을 달려면 로그인하십시오.


Manolis Michailidis
Manolis Michailidis 2015년 10월 13일
편집: Manolis Michailidis 2015년 10월 13일
Lets assume x(1x3) and y(1x8)
Padd=[x,zeros(1,length(y)-length(x))];
  댓글 수: 2
Chameleon17
Chameleon17 2015년 10월 13일
편집: Chameleon17 2015년 10월 13일
OH!!!! Thank you thank you thank you!!!
That's just great! Thank you so much!!!
Manolis Michailidis
Manolis Michailidis 2015년 10월 13일
If it helped please accept the answer

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by