If conditions for matrices with different length

2 次查看(过去 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 个评论
Dixi
Dixi 2019-6-28
Maybe "compare" isn't the right word as an if condition always compares parameters.
Correct. The values of T1.A and T2.A as well as T2.A and T2.B are identical in parts. T2.A and T2.B are a subset of T1.A and T1.B. I want to check if there is an identic combination of values T2.A and T2.B in T1.A and T1.B while T1.C has to be "1".
Stephen23
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
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 个评论
Stephen23
Stephen23 2019-6-28
@Dixi: I am sure that you can adapt the answer by calling ismember twice: once for the concatenated datetime arrays, and once for the ones.
Alex Mcaulley
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 CenterFile Exchange 中查找有关 Logical 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by