Help, the calculation provides 0 for all cells. why calling the formula on the command line it provides the right answer for the line item, but once put into a loop the answers is 0 for all cells in N.
for s=1:34
TR1{s}=abs((prices{1,3}{1,s}(2:end))-(prices{1,4}{1,s}(2:end)));
TR2{s}=abs((prices{1,3}{1,s}(2:end))-(prices{1,5}{1,s}(1:end-1)));
TR3{s}=abs((prices{1,5}{1,s}(1:end-1))-(prices{1,4}{1,s}(2:end)));
TR4{s}=[TR1{1,s} TR2{1,s} TR3{1,s}];
TR{s}=max(TR4{1,s},[],2);
end
% N (Average True Range)
for i=1:length(TR)
for t=length(TR{i})
if t<21
N{i}(t,1)=mean(TR{i}(1:t,1));
else
N{i}(t,1)=((19*(TR{i}(t-1,1)))+TR{i}(t,1))/20;
end
end
end
See attached for prices.mat

 Akzeptierte Antwort

KSSV
KSSV am 15 Dez. 2016
Bearbeitet: KSSV am 15 Dez. 2016

0 Stimmen

All fine...only a minor change:
clc; clear all ;
load prices.mat ;
for s=1:34
TR1{s}=abs((prices{1,3}{1,s}(2:end))-(prices{1,4}{1,s}(2:end)));
TR2{s}=abs((prices{1,3}{1,s}(2:end))-(prices{1,5}{1,s}(1:end-1)));
TR3{s}=abs((prices{1,5}{1,s}(1:end-1))-(prices{1,4}{1,s}(2:end)));
TR4{s}=[TR1{1,s} TR2{1,s} TR3{1,s}];
TR{s}=max(TR4{1,s},[],2);
end
% N (Average True Range)
% N = cell(length(TR),1) ;
for i=1:length(TR)
for t=1:length(TR{i})
if t<21
N{i}(t,1)=mean(TR{i}(1:t,1));
else
N{i}(t,1)=((19*(TR{i}(t-1,1)))+TR{i}(t,1))/20;
end
end
end

4 Kommentare

liu James
liu James am 15 Dez. 2016
Hey KSSV. much appreciated in helping, but its still not what I'm looking for. I'm trying to have it return all N for the sub cells of all 1:34 columns. This provides the same answer when I did it without the extra indexing in N.
KSSV
KSSV am 15 Dez. 2016
Not clear...you want N for TR1,TR2 etc?
KSSV
KSSV am 15 Dez. 2016
Check your t loop, it should be t=1:length(TR{i})...I have edited the answer....is that you are expecting?
liu James
liu James am 15 Dez. 2016
thanks much appreciated!!!!! it was driving me nuts.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange

Tags

Gefragt:

am 15 Dez. 2016

Kommentiert:

am 15 Dez. 2016

Community Treasure Hunt

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

Start Hunting!

Translated by