If conditions for matrices with different length

조회 수: 2 (최근 30일)
Dixi
Dixi 2019년 6월 27일
댓글: Alex Mcaulley 2019년 6월 28일
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