removing element from all arrays in cell array of 1-dimensional arrays

조회 수: 1 (최근 30일)
I have a cell array out = cell(n,1), and each out{j}, 1<= j <= n, is a vector of integers. These vectors have different lengths. How can I efficiently remove an integer k from all vectors containing k?

채택된 답변

Andrei Bobrov
Andrei Bobrov 2012년 5월 4일
newout = cellfun(@(x)setdiff(x,k),out,'un',0);

추가 답변 (1개)

Jan
Jan 2012년 5월 4일
for i = 1:n
v = out{i};
out{i} = v(v ~= k);
end
I assume 1. that a smarter cellfun approach is slower and 2. that spending more time for programming a faster solution (if there is any) will not pay off, except that this function is called trillions of times.
  댓글 수: 1
Kees Roos
Kees Roos 2012년 5월 4일
Thanks! Your remark on cellfun turns out to be true. But in my case, where n=1000, the for loop takes also too much time. I was hoping for a 'vectorized' solution.

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by