Delete cells that satisfy a specific condition

I have an array of cells, each of which is an n-by-n matrix. I want to remove cells for which inv(cell{i}'*cell{i}) indicates that the matrix is ​​close to unity. thank you

+4
source share
3 answers

In general, removing elements is the easy part. If C is your array, removing the cells indicated by the indices in the idx vector can be done with:

 C(idx) = {}; 

As for your specific problem, checking if the matrix is β€‹β€‹β€œalmost” unique or not can be done using rcond (if the result is close to zero, it is probably singular). To apply it to all cells, you can use cellfun as follows:

 idx = cellfun(@(x)(rcond(x' * x) < 1e-12), C); 

Adjust the threshold value as you wish. The resulting idx is a logical array with 1 at the location of the singular matrices. Use idx to remove these elements from C , as shown above.

+3
source

Create a function that checks your status:

 function state = CheckElement(element) if ([condition]) state = 1; else state = 0; end end 

then execute cellfun for all elements of the cell array, such as:

 indices = cellfun(@CheckElement,myCellArray); cellArray(indices ) = []; 
+1
source

Assuming you have already defined a specific issingular function, you can use cellfun to get cell indices containing the matrices you want to remove.

 c; % cell arry singularIdx = cellfun((@x) issingular( inv(x' * x)), c); %' added single quote for SO syntax cFiltered = c(~singluarIdx); 

You probably need to write your own function to check the singularity, for more information see this question: Quick check method, is the Matrix unique? (not reversible, det = 0)

0
source

All Articles