Is there a way to change a vector range by condition?

Hallo,
i use a simple example for my problem:
i have a vector
x=[-10:10];
x=x';
Now i want to change values of the range 5:15 which are positive. i tried something like this:
x(5:15 & x>0,1)=100
x( (5:15) & (x>0) ,1)=100
x(5:15,1)&x>0=100
x(x(5:15)<0,1)=100
nothing seems to work. pls help.

 채택된 답변

Andrei Bobrov
Andrei Bobrov 2016년 10월 12일
편집: Andrei Bobrov 2016년 10월 12일
x=(-10:10)';
z = x(5:15);
z(z>0) = 100;
x(5:15) = z;
or
x=(-10:10)';
b = false(size(x));
b(5:15) = true;
x(b & x > 0) = 100;
or in one row
x(intersect(find(x>0),5:15)) = 100;

댓글 수: 1

thanks. not a one row command but this is what I needed.

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

추가 답변 (1개)

KSSV
KSSV 2016년 10월 12일
If you want to change values at known indices use:
x(5:15)=100;
If you want to change values according to the values of x use:
x(x>1)=100;
x(x>1 & x<2)=100;

댓글 수: 1

maxmathlab
maxmathlab 2016년 10월 12일
편집: maxmathlab 2016년 10월 12일
i know matlab this far. but how do i combind it?
for e.g.: if i want to change my vector x from x(5) to x(15) but also only negativ values, what would the command look like?

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

카테고리

도움말 센터File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

질문:

2016년 10월 12일

편집:

2016년 10월 12일

Community Treasure Hunt

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

Start Hunting!

Translated by