removing cells that satisfy a specific condtion

Hello everyone. I have a cell array, from which the cells contain a matrix. I want to remove the cells for which, lets say inv(cell{i}'*cell{i}) gives a warning message that matrix is almost singular. is there a way to do this? I would really appreciate if someone knows how to do this

回答 (1 件)

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 1 月 15 日
編集済み: Azzi Abdelmalek 2013 年 1 月 15 日

0 投票

for k=1:numel(A) % A is you cell array
try
a=inv(A{k}'*A{k})
out{k}=A{k} % out is the result
catch err
end
end

5 件のコメント

Christos
Christos 2013 年 1 月 15 日
Azzi thank you for your help, but i cannot understand how to use this. I dont want to ignore the message. I want to remove the cells that give the warning message. For example if my initial cell array has length 10, if two of the cells contain a matrix that is almost singular (so the warning message comes up), then the output should be of length 8. The initial cells array without the two cells that give almost singular inv(A{k}'*A{k}).
I hope this helps.
Brian B
Brian B 2013 年 1 月 15 日
編集済み: Brian B 2013 年 1 月 15 日
If you really need to cause a warning:
k=1;
while k<=numel(A) % A is you cell array
try
a=inv(A{k}'*A{k})
k=k+1;
catch exception
A(k) = [];
end
end
EDIT: Actually, this would only catch errors, not warnings. You could devise a solution using lastwarn, but there is a better way, assuming you just want to remove elements which result in nearly-singular products:
CondTol = 1e12; % select how close a.'*a must be to singular
nearSingular = cellfun(@(a) cond(a'*a)>CondTol, A);
A = A(~nearSingular);
Brian B
Brian B 2013 年 1 月 15 日
If you really need to act based on a warning, here is one way, though I don't like it at all:
warning('OK')
k=1;
while k<=numel(A) % A is you cell array
a=inv(A{k}'*A{k});
if ~strcmp(lastwarn,'OK')
A(k) = [];
warning('OK')
else
k=k+1;
end
end
Brian B
Brian B 2013 年 1 月 15 日
You could also use Azzi's code if you force the warning you expect to be handled like an error, as discussed at http://undocumentedmatlab.com/blog/trapping-warnings-efficiently/.
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 1 月 15 日
編集済み: Azzi Abdelmalek 2013 年 1 月 15 日
look at this example
A={[1 2 3 0 0 0; 0 0 0 0 0 0], randi(16,4)}
l=0;
for k=1:numel(A) % A is you cell array
try
a=inv(A{k})
l=l+1
out{l}=A{k} % out is the result
catch err
end
end
out
% the first matrix is not square, can't be reversed, then it will not apear in out array.
% I am not sure if the message "matrix is almost singular" is considered as an error mesage

この質問は閉じられています。

タグ

質問済み:

2013 年 1 月 15 日

閉鎖済み:

2021 年 8 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by