column multiplication by specific number with some condition

조회 수: 2 (최근 30일)
Nazar Tarlanli
Nazar Tarlanli 2018년 11월 14일
댓글: madhan ravi 2018년 11월 15일
r = -5 + (5+5)*rand(10,1) % column with positive and negative numbers
I want to create the function r_1 by satisfying the following:
r_1= r .* 0.5 if r >0
and
r_1 = r .* 0.7 if r<0
I guess, I should multiply positive values by 0.5 and negative values by 0.7.

답변 (1개)

madhan ravi
madhan ravi 2018년 11월 15일
편집: madhan ravi 2018년 11월 15일
r_1=ones(1,numel(r)):
r_1(r>0)=r(r>0).*.5;
r_1(r<0)=r(r<0).*.7;
r_1
  댓글 수: 2
Nazar Tarlanli
Nazar Tarlanli 2018년 11월 15일
편집: Nazar Tarlanli 2018년 11월 15일
r_1=zeros(size(r));
r_1(r>0)=r(r>0).*.5;
r_1(r<0)=r(r<0).*.7;
r_1
I did run this code,it work. Thanks
madhan ravi
madhan ravi 2018년 11월 15일
Anytime :) make sure to accept the answer if it worked

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

카테고리

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

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by