# How to calculate value using different matrix using if and for loop

조회 수: 1(최근 30일)
Adhish Aggarwal 2017년 4월 19일
댓글: Adhish Aggarwal 2017년 4월 19일
I want to calculate the value of up5 and down5 with the given condition. Each cell of up5 or down5 should be calculated separately by following the condition. But it shows only one type either std5*2 + avg5 or std5*3 + avg5 and not the combined answer.
b = AXIS(1:100,1) - KOTAK(1:100,1);
avg5 = zeros(5,1);
for k = 5:length(b)
avg5(k) = mean(b(k-4:k,1));
end
a = 0;
avg5 = [a;avg5];
avg5(numel(avg5),:) = [];
std5 = zeros(5,1);
for al = 5:length(b)
std5(al) = std(b(al-4:al,1));
end
% a = 0;
std5 = [a;std5];
std5(numel(std5),:) = [];
up5 = zeros(length(std5),1);
for d = 1 : length(std5)
if abs(b) < 14.5
up5(d) = std5*2 + avg5;
down5(d) = std5*(-2) + avg5;
else
up5(d) = std5*2 + avg5;
down5(d) = std5*(-2) + avg5;
end
end

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

### 채택된 답변

Andrew Newell 2017년 4월 19일
In the line
if abs(b) < 14.5
the condition is looking at the entire 100 x 1 vector b, and the condition is only true if all the entries are less than 14.5. There is no dependence on d. Perhaps you want this?
if abs(b(d)) < 14.5
##### 댓글 수: 1표시숨기기 없음
Adhish Aggarwal 2017년 4월 19일
it worked thanx a lot.

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

### 범주

Find more on Startup and Shutdown in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by