Can Matlab cellular functions -like cellfun- work with non linearly spaced indices ?
조회 수: 2 (최근 30일)
이전 댓글 표시
Hi,
I would like to affect a value to some non linearly indexed elements of a vector with using a cell array.
At the moment I index a for loop with a non linearly spaced row index. Let's say :
N = 8;
id = find(randi(2,1,N)-1); % non zeros positions in a random vector of binaries
u = true(1,N);
for k = id
u(1,k:k:end) = false;
end
How would I do the same affectations with cellfun for instance ?
Thank you for help.
Cheers.
Nicolas
댓글 수: 0
채택된 답변
dpb
2024년 6월 18일
Use direct indexing, no looping needed...
N = 8;
u = true(1,N);
id = find(randi(2,1,N)-1); % non zeros positions in a random vector of binaries
id
u(id)=false;
u
NOTA BENE: if u is a multi-dimensional array, it will be necessary to use sub2ind and ind2sub to get linear indices from the positions or use the ".*" operation with the id array the same size as u, writing the single id vector for a 2D or higher array won't work as expected as it will be interpreted as the linear index.
댓글 수: 6
dpb
2024년 6월 18일
N = 20;
u = true(1,N);
for i=1:5
id = find(randi(2,1,N)-1); % non zeros positions in a random vector of binaries
numel(id)
id
end
Of course, the one thing one can do is check if the value "1" shows up in id; if so then that's a special case that
if id(1)==1, u=~u; end
After that, if begins with 2, all higher even indices can be removed from the index array because they'll have already been cleared when get there.
After that, can check for multiples of the next; the third example above could remove 12 because it is a multiple of 3 (or 4).
I think only experimentation would reveal whether doing such preliminary work beyond this would be an overall optimization or not.
dpb
2024년 6월 18일
I guess the thing one could do would be to keep all primes and then decimate the others.
That actually might not be too bad of an exercise to implement.
추가 답변 (2개)
Steven Lord
2024년 6월 18일
So you're sieving? What is the real value of N for which you want to solve this problem, and how many values are you trying to sieve out (how many elements does id have?)
N = 8;
id = find(randi(2,1,N)-1) % non zeros positions in a random vector of binaries
u = true(1,N);
for k = id
u(1,k:k:end) = false;
end
multiples = mod(1:N, id.') == 0
toKeep = ~any(multiples, 1)
u
댓글 수: 0
Nicolas Douillet
2024년 6월 19일
편집: Nicolas Douillet
2024년 6월 19일
댓글 수: 1
dpb
2024년 6월 19일
편집: dpb
2024년 6월 19일
Replacing the loop itself with arrayfun or cellfun will almost certainly only increase CPU time over the direct loop.
The fundamental problem is your sieve doesn't have an analytical expression that can be evaluated; the only way to produce the indices is iteration and the explicit loop is almost certainly going to be faster than any more complex method. All the xxxxfun() functions do is move the loop below the visible user code but it still has to get translated to a loop to actually do the deed....
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!