Suppose if I have cell array a{1}=[1 1 0]; a{2}=[0 0 0]; a{3}=[0 0 0]. I want to check if any elements of the cell array are equal. If i use " isequal(a{:}) " then it returns "Logical 0". However, we can see that a{2} == a{3}. So, i need the answer to be "Logical 1" as some of the elements of the cell array are equal. Is there any built-in function to check that directly in MATLAB.
MATLAB: Check if any elements of cell array are equal
cell arraycell arraysMATLAB
Related Solutions
"...however if I try to apply the same command again as..."
You have failed to take into account that inbetween running these two commands you have changed the data. So while the first cell array only contained scalar numeric (which is a bad way to store numeric data), the second time you call cellfun the cell array now also contains char vectors (lots of 'false', to be precise). Of course when you compare 'false'==1 then the output will be a vector like this:
>> 'false'==1ans = 0 0 0 0 0
and that is clearly not a scalar value, so your cellfun usage will fail unless you use 'uni',false and some more complex processing, or wrap the first argument function in any or something similar. Your third example give this error message
Function 'subsindex' is not defined for values of class 'cell'
because when you use the option 'UniformOutput',false then cellfun outputs a cell array, which cannot be used for indexing. So your attempt to use the output as an index will fail.
Here is a simpler approach using a numeric array (which it should be anyway):
>> m = cell2mat(a);>> id0 = m==0;>> id1 = m==1;>> a(id0) = {'false'};>> a(id1) = {'true'};>> aa = 'false' 'true' [ 2] [ 3] [ 4] [ 5] [ 6] [ 7] [ 8] [ 9] [ 10] [ 11] 'true' 'false' 'false' 'false' 'false' 'false' 'false' 'false' 'false' 'false' 'false' 'false' [ 2] 'true' 'true' 'true' 'false' 'false' 'true' 'false' 'false' 'false' 'false' 'false' [ 3] 'true' 'true' 'true' 'false' 'false' 'true' 'false' 'false' 'false' 'false' 'false' [ 4] 'true' 'true' 'true' 'false' 'false' 'true' 'false' 'false' 'false' 'false' 'false' [ 5] 'true' 'true' 'true' 'false' 'false' 'true' 'false' 'false' 'false' 'false' 'false' [ 6] 'true' 'true' 'true' 'false' 'false' 'true' 'false' 'false' 'false' 'false' 'false' [ 7] 'true' 'true' 'true' 'false' 'false' 'true' 'false' 'false' 'false' 'false' 'false' [ 8] 'true' 'true' 'true' 'false' 'false' 'true' 'false' 'false' 'false' 'false' 'false' [ 9] 'true' 'true' 'true' 'false' 'false' 'true' 'false' 'false' 'false' 'false' 'false' [ 10] 'true' 'true' 'true' 'false' 'false' 'true' 'false' 'false' 'false' 'false' 'false' [ 11] 'true' 'true' 'true' 'false' 'false' 'true' 'false' 'true' 'false' 'false' 'false'
Note that your row and column headers indicate that you should perhaps be using a table, or only storing the actual data (not the headers) in a simple numeric array. Then you could do this task very simply with some basic indexing:
>> c = {'false','true'};>> b = c(1+m(2:end,2:end))b = 'false' 'false' 'false' 'false' 'false' 'false' 'false' 'false' 'false' 'false' 'false' 'true' 'true' 'true' 'false' 'false' 'true' 'false' 'false' 'false' 'false' 'false' 'true' 'true' 'true' 'false' 'false' 'true' 'false' 'false' 'false' 'false' 'false' 'true' 'true' 'true' 'false' 'false' 'true' 'false' 'false' 'false' 'false' 'false' 'true' 'true' 'true' 'false' 'false' 'true' 'false' 'false' 'false' 'false' 'false' 'true' 'true' 'true' 'false' 'false' 'true' 'false' 'false' 'false' 'false' 'false' 'true' 'true' 'true' 'false' 'false' 'true' 'false' 'false' 'false' 'false' 'false' 'true' 'true' 'true' 'false' 'false' 'true' 'false' 'false' 'false' 'false' 'false' 'true' 'true' 'true' 'false' 'false' 'true' 'false' 'false' 'false' 'false' 'false' 'true' 'true' 'true' 'false' 'false' 'true' 'false' 'false' 'false' 'false' 'false' 'true' 'true' 'true' 'false' 'false' 'true' 'false' 'true' 'false' 'false' 'false'
z =@(x) tan(x);h = 0.5x=1:0.5:10Output = zeros(1,19) %preallocation for speed and efficiency
for i = 1:numel(x) Output (i) = (z(x(i)+h)-z(x(i)-h))/(2*h);endArray = Output
Related Question
- Create matrix of vector from 3 differents cell array
- Storing the index of an Matrix for non zeros
- Multiplication of cell arrays
- How many times each element of an array have the 1 value after n times assigned different logical values
- Problem with function handle
- Subtraction on the cells elements
- 3 equations 4 unknowns
Best Answer