Can Matlab cellular functions -like cellfun- work with non linearly spaced indices ?
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Nicolas Douillet
el 18 de Jun. de 2024
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 comentarios
Respuesta aceptada
dpb
el 18 de Jun. de 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
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 comentarios
dpb
el 18 de Jun. de 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
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
el 18 de Jun. de 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.
Más respuestas (2)
Steven Lord
el 18 de Jun. de 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
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 comentarios
Nicolas Douillet
el 19 de Jun. de 2024
Editada: Nicolas Douillet
el 19 de Jun. de 2024
1 comentario
dpb
el 19 de Jun. de 2024
Editada: dpb
el 19 de Jun. de 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....
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!