Filter löschen
Filter löschen

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

2 Ansichten (letzte 30 Tage)
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

Akzeptierte Antwort

Andrew Newell
Andrew Newell am 19 Apr. 2017
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

Weitere Antworten (0)

Kategorien

Mehr zu Operators and Elementary Operations finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by