필터 지우기
필터 지우기

How to check if all the data in the cell array are the same?

조회 수: 49 (최근 30일)
niniki
niniki 2022년 3월 21일
댓글: Jan 2022년 3월 21일
There is a variable called 'data'
in a cell array of 1x10
{100} {100} {100} {100} {100} {100} {100} {100} {100} {100} 100 has the same value.
How can I return these 10 data to True or False?
isequal(data(1),data(2),data(3),data(4),data(5),data(6),data(7),data(8),data(9),data(10))
Instead of writing down all the elements like this, I want to make sure that all the data in th
e cell array has the same value even if the size of the cell array changes.

답변 (3개)

Stephen23
Stephen23 2022년 3월 21일
편집: Stephen23 2022년 3월 21일
The simple and very efficient MATLAB approach is to use a comma-separated list:
Avoid approaches which join the data together (e.g. CELL2MAT) or that change data type (e.g. to STRING): they are inefficient, fragile, and will fail for arrays of incompatible sizes, types, or due to loss of information when converting type.
C = repmat({10},1,9)
C = 1×9 cell array
{[10]} {[10]} {[10]} {[10]} {[10]} {[10]} {[10]} {[10]} {[10]}
X = isequal(C{:})
X = logical
1
C{1} = 10+eps(10)
C = 1×9 cell array
{[10.0000]} {[10]} {[10]} {[10]} {[10]} {[10]} {[10]} {[10]} {[10]}
X = isequal(C{:})
X = logical
0

Chunru
Chunru 2022년 3월 21일
c = {100, 100, 100, 200} % cell array
c = 1×4 cell array
{[100]} {[100]} {[100]} {[200]}
cm = cell2mat(c);
same = all(cm == cm(1))
same = logical
0

Arif Hoq
Arif Hoq 2022년 3월 21일
if you want to find every index for true or false
A={100};
B=repmat(A,1,10);
[Lia Locb]=ismember(string(B),string({100}))
Lia = 1×10 logical array
1 1 1 1 1 1 1 1 1 1
Locb = 1×10
1 1 1 1 1 1 1 1 1 1
  댓글 수: 1
Jan
Jan 2022년 3월 21일
The conversion to a string is an expensive indirection. isequal(A{:}) is faster.

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

카테고리

Help CenterFile Exchange에서 Cell Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by