Can Matlab cellular functions -like cellfun- work with non linearly spaced indices ?

1 view (last 30 days)
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

Accepted Answer

dpb
dpb on 18 Jun 2024
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
id = 1x4
4 5 6 7
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
u(id)=false;
u
u = 1x8 logical array
1 1 1 0 0 0 0 1
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 Comments
dpb
dpb on 18 Jun 2024
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
ans = 10
id = 1x10
1 2 5 7 8 12 15 16 18 19
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
ans = 13
id = 1x13
1 2 4 6 7 9 13 14 15 16 17 18 19
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
ans = 8
id = 1x8
3 4 7 10 12 13 17 19
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
ans = 11
id = 1x11
3 5 7 8 9 10 11 12 13 15 18
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
ans = 10
id = 1x10
3 7 8 11 12 13 15 17 19 20
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
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
dpb on 18 Jun 2024
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.

Sign in to comment.

More Answers (2)

Steven Lord
Steven Lord on 18 Jun 2024
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
id = 1x2
4 5
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
u = true(1,N);
for k = id
u(1,k:k:end) = false;
end
multiples = mod(1:N, id.') == 0
multiples = 2x8 logical array
0 0 0 1 0 0 0 1 0 0 0 0 1 0 0 0
toKeep = ~any(multiples, 1)
toKeep = 1x8 logical array
1 1 1 0 0 1 1 0
u
u = 1x8 logical array
1 1 1 0 0 1 1 0

Nicolas Douillet
Nicolas Douillet on 19 Jun 2024
Edited: Nicolas Douillet on 19 Jun 2024
Thank you dpb and Steven Lord for your answers. I am going to test your solutions. Edit : yes indeed it is sieving. N may then take great integer values. I am still interesting to know in the end if my for loop can be replaced by a cellular function (cellfun, rowfun, etc...). The goal is of course to try to save cpu time.
  1 Comment
dpb
dpb on 19 Jun 2024
Edited: dpb on 19 Jun 2024
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....

Sign in to comment.

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!