For Loop that Checks 2 Matrices to create a Combined New one based on IF Statement
1 回表示 (過去 30 日間)
古いコメントを表示
Good Morning,
I have the following statement that check 3 data sets to combine an answer (1,0,-1) but i am not able to get it to work. I am new in MATLAB and i am able to get an excel statement that works (attached in file).
sOpen = zeros(size(sOpen));
sClose = zeros(size(sOpen));
sCombined = zeros(size(sOpen));
for k = 2:numel(sOpen)
kk = sOpen(k)~=0 && sClose(k)~=0; % To check if there is a value -1 or 1 in sOpen and sClose
if all(kk)
kkk = sCombined(k-1)==-1 & sClose(k) ==1; % If prior combined result equals -1 and current CLOSE = 1 then result = 1
sCombined(k) = 1;
elseif all(~kkk)
kkkk = sCombined(k-1)==0 & sOpen(k) ==-1; % If prior combined result equals 0 and current OPEN = -1 then result = -1
sCombined(k) = -1;
elseif all(~kkk)
kkkkk = sCombined(k-1)==-1 & sClose(k) ==0; % If prior combined result equals -1 and current CLOSE = 0 then result = -1
sCombined(k) = -1;
else
sCombined(k) = 0; %Anything else should be 0
end
end
0 件のコメント
採用された回答
Stephen23
2022 年 2 月 9 日
編集済み: Stephen23
2022 年 2 月 9 日
Here is a direct translation of your Excel formula:
T = readtable('Data.xlsx') % import your data
nmr = height(T);
out = zeros(nmr,1);
for k = 2:nmr
% IF(AND(E1=-1,B2=1),1,IF(AND(E1=0,A2=-1),-1,IF(AND(E1=-1,B2=0),-1,0)))
if out(k-1)==-1 && T.Sclose(k)==1
out(k) = 1;
elseif out(k-1)==0 && T.Sopen(k)==-1
out(k) = -1;
elseif out(k-1)==-1 && T.Sclose(k)==0
out(k) = -1;
end
end
isequal(out,T.CorrectAnswer)
isequal(out,T.ExcelStatement)
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!