For Loop that Checks 2 Matrices to create a Combined New one based on IF Statement
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Respuesta aceptada
Stephen23
el 9 de Feb. de 2022
Editada: Stephen23
el 9 de Feb. de 2022
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)
Más respuestas (0)
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!