필터 지우기
필터 지우기

How to generate new vector and replace certain elements?

조회 수: 2 (최근 30일)
Quick Mart
Quick Mart 2020년 2월 20일
댓글: Quick Mart 2020년 2월 20일
I have vector X = [3 15 9 12 -1 0 -12 9 6 1].
I want to generate a new vector V which equals X except that the elements of X that are greater than the mean of X are replaced by their difference from the mean of X.
I've tried doing this with
V = X;
V=V(V>mean(V))-mean(V)
But it generates only
10.8 4.8 7.8 4.8 1.8
it doesn't keep the values that are less than the mean.
I'm new to Matlab and have tried approaching it with a loop but I just can't figure it out.

채택된 답변

Guillaume
Guillaume 2020년 2월 20일
Nearly got it:
meanV = mean(V); %save the mean in a new variable to avoid calculating it more than once
V(V > meanV) = V(V > meanV) - meanV;

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by