If conditions for matrices with different length
1 次查看(过去 30 天)
显示 更早的评论
Hello MatLab users,
i found some edits to this topic but my code is still not running and a for-loop with if-condition causes "Operands to the || and && operators must be convertible to logical scalar values".
A and B have to be compared for equality while C has to be "1".
Conditions are:
T1.A == T2.A
T1.B == T2.B
T1.C == 1
than T2.Output = 1
else T2.Output = 0
T1.A = [01-Jan-2019 00:00:00; 01-Jan-2019 00:00:00; 01-Jan-2019 00:00:00; 01-Jan-2019 00:00:00; 01-Jan-2019 00:00:00; 02-Jan-2019 00:00:00; 02-Jan-2019 00:00:00; 02-Jan-2019 00:00:00; 02-Jan-2019 00:00:00; 02-Jan-2019 00:00:00; 03-Jan-2019 00:00:00; 03-Jan-2019 00:00:00]
T1.B = [1; 1; 2; 2; 2; 1; 1; 1; 2; 2; 1; 1]
T1.C = [0; 0; 0; 1; 0; 1; 0; 0; 0; 0; 1; 0]
T2.A = [01-Jan-2019 00:00:00; 01-Jan-2019 00:00:00; 02-Jan-2019 00:00:00; 02-Jan-2019 00:00:00; 03-Jan-2019 00:00:00]
T2.B = [1; 2; 1; 2; 1]
Output should be:
T2.Output = [0; 1; 1; 0; 1]
Thanks!
3 个评论
Stephen23
2019-6-28
@Dixi: please stop editing your question so that the answers no longer make any sense. Your original data was valid numeric data, but what you have just have replaced them with are not valid MATLAB syntax, so are not very useful for anyone. If you decide that you want to give the correct datetime arrays then simply upload your data in one mat file, by clicking the paperclip button.
采纳的回答
Alex Mcaulley
2019-6-28
I think you mean this (Note that the second element of T2 doesn't meet your conditions! as you posted as the expected output):
T2.Output = ismember([T2.A,T2.B,ones(numel(T2.A),1)],[T1.A,T1.B,T1.C],'rows');
ans =
5×1 logical array
0
0
1
0
1
5 个评论
Alex Mcaulley
2019-6-28
Or just using datenum:
T2.Output = ismember([datenum(T2.A),T2.B,ones(numel(T2.A),1)],[datenum(T1.A),T1.B,T1.C],'rows');
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!