Fill a vector with zeros

조회 수: 45 (최근 30일)
Carlos Martínez
Carlos Martínez 2015년 11월 9일
댓글: Star Strider 2015년 11월 9일
I have a vector difined as:
n=n1:n2
I want to make another vector of the same size as n called x, but with just a n0 value, and the others as zeros. Example:
n0=1
n1=-2
n2=2
So the vector n will be n = [-2 -1 0 1 2] and the x vector should be: x = [0 0 0 1 0]
Any idea aboud how can I do that?

답변 (2개)

TastyPastry
TastyPastry 2015년 11월 9일
n(n~=n0) = 0;

Star Strider
Star Strider 2015년 11월 9일
One approach:
n0=1;
n1=-2;
n2=2;
n=n1:n2;
nz = zeros(size(n));
nz(n==n0) = n0
nz =
0 0 0 1 0
  댓글 수: 2
Carlos Martínez
Carlos Martínez 2015년 11월 9일
¿What about if i want that since the n0 value every value is n0?
Star Strider
Star Strider 2015년 11월 9일
If you want every value of the new vector to be ‘n0’, the new vector becomes:
nz = ones(size(n))*n0;
Please give an example of what you want if it is something other than what I describe here.

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

카테고리

Help CenterFile Exchange에서 Introduction to Installation and Licensing에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by