Check if all elements of cell array are equal to a certain value?

43 Ansichten (letzte 30 Tage)
Thadeus
Thadeus am 10 Apr. 2023
Kommentiert: Philipp am 6 Dez. 2024
Hello,
I have been trying find a way to check if all the elements of my cell array are equal to certain value.
I need help on how someone can perform this task.
  1 Kommentar
Philipp
Philipp am 6 Dez. 2024
If the value does not matter you can use unique and test if the number of elements in the result is 1

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Ran Yang
Ran Yang am 10 Apr. 2023
If your cell array only contains numbers, then you should convert it to a regular array using cell2mat, then test equality using ==.
x = {2 5; 3 5}; % example cell array
y = cell2mat(x);
all(y == 5, 'all') % check if all entries are 5

Praveen Reddy
Praveen Reddy am 10 Apr. 2023
Hi Thadeus,
I understand that you want to compare all the values of a cell array against some value. You can use “isequal” function to achieve this. First check if all the cell array values are equal and then compare any of the cell array value against the desired value.
A = repmat({10},1,9);
B = {[10,10,1],[10,10,1],[10,10,1]};
logicalVal1=(isequal(A{:}) && A{1}==10); % logicalValue outputs to true
logicalVal2=(isequal(B{:}) && isequal(B{1},[10,10,1]));% logicalValue outputs to true
logicalVal3=(isequal(B{:}) && isequal(B{1},[10,0,1]));% logicalValue outputs to false
To know more about “isequal” function, please refer to the following MATLAB documentation:

Kategorien

Mehr zu Operators and Elementary Operations finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by