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

1 次查看(过去 30 天)
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
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Operators and Elementary Operations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by