If conditions for matrices with different length

2 views (last 30 days)
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 Comments
Dixi
Dixi on 28 Jun 2019
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 on 28 Jun 2019
@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.

Sign in to comment.

Accepted Answer

Alex Mcaulley
Alex Mcaulley on 28 Jun 2019
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 Comments
Stephen23
Stephen23 on 28 Jun 2019
@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 on 28 Jun 2019
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');

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!