# Combine logical cell array based on 'or' condition

조회 수: 31(최근 30일)
Alex Wooten 2021년 5월 21일
댓글: Alex Wooten 2021년 5월 21일
I am trying to combine all elements of a cell array containing multiple logical arrays into a single array, where the single array has a true value if any of the logical arrays contain true at that index.
C{1,1} = logical([1;0;0;0;0]);
C{1,2} = logical([0;1;0;0;0]);
C{1,3} = logical([0;0;1;0;0]);
I know that if I wanted to combine 2 of the elements, I could do this:
or(C{1,1}, C{1,2})
This would give me the output:
ans =
5×1 logical array
1
1
0
0
0
Is there a way to combine all 3 logical arrays and get this as output? (preferably in a single operation)
ans =
5×1 logical array
1
1
1
0
0

댓글을 달려면 로그인하십시오.

### 채택된 답변

DGM 2021년 5월 21일
Maybe something like this:
% test array
C{1,1} = logical([1;0;0;0;0]);
C{1,2} = logical([0;1;0;0;0]);
C{1,3} = logical([0;0;1;0;0]);
Cl = any(cell2mat(C),2)
Cl = 5×1 logical array
1 1 1 0 0
##### 댓글 수: 1표시숨기기 없음
Alex Wooten 2021년 5월 21일
Exactly what I was looking for, thanks!

댓글을 달려면 로그인하십시오.

### 추가 답변(2개)

Daniel Bengtson 2021년 5월 21일
You could do something like this.
C{1,1} = logical([1;0;0;0;0]);
C{1,2} = logical([0;1;0;0;0]);
C{1,3} = logical([0;0;1;0;0]);
[~,wid] = size(C);
%false vector to use as a starting point
init = false(size(C{1,1}));
%ORs together every vector in C
for i = 1:wid
init = init | C{1,i};
end
##### 댓글 수: 0표시숨기기 이전 댓글 수: -1

댓글을 달려면 로그인하십시오.

David Fletcher 2021년 5월 21일
C{1,1}|C{1,2}|C{1,3}

댓글을 달려면 로그인하십시오.

### 범주

Find more on Introduction to Installation and Licensing in Help Center and File Exchange

R2021a

### Community Treasure Hunt

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

Start Hunting!