If conditions for matrices with different length

1 visualizzazione (ultimi 30 giorni)
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 Commenti
Dixi
Dixi il 28 Giu 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 il 28 Giu 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.

Accedi per commentare.

Risposta accettata

Alex Mcaulley
Alex Mcaulley il 28 Giu 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 Commenti
Stephen23
Stephen23 il 28 Giu 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 il 28 Giu 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');

Accedi per commentare.

Più risposte (0)

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by