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
0 件のコメント
採用された回答
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 Center および File Exchange で Operators and Elementary Operations についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!