how velocize this code

%ss is array double
%profit is array double
g=zeros(height(ss));
maxCat=3;
for i=1:height(ss)
z=find(ss(:,i)>0);
if ~isempty(z)
[val,idx]=sort(profit(z),'descend');
ret=idx(1:maxCat);
g(ret,i)=1;
end
end
thank you

8 comentarios

Rik
Rik el 13 de Jul. de 2023
This time I edited your question for you. Next time, please use the tools explained on this page to make your question more readable.
You might consider using max three times, instead of sorting (although that might not make much difference, it will depend on your data characteristics).
Another small tweak: you don't need find, since you can use ~any(z) and use z directly to index the result.
A final remark: you code looks like a change in algorithm would probably result in a much more substantial speed increase. I'm not entirely sure yet how, but I suspect there is a possibility for a different setup.
It would help if you either attach your data in a mat file, or write code that will generate plausible data.
shamal
shamal el 13 de Jul. de 2023
Editada: shamal el 13 de Jul. de 2023
i meant to use cellfun (which i don't know how to use)..it should result in a performance improvement
(i want to avoid to use loop)
Walter Roberson
Walter Roberson el 13 de Jul. de 2023
Instead of using max three times, you can use maxk
I notice that you check isempty(z) but then assume that there are maxCat values available in profit(z) . What happens if the number of ss(i,i)>0 values is 1 or 2, so that profit(z) is only 1 or 2 entries instead of maxCat==3 entries?
Walter Roberson
Walter Roberson el 13 de Jul. de 2023
i meant to use cellfun (which i don't know how to use)..it should result in a performance improvement
In most cases, using cellfun() is slower than a loop. When you use cellfun, you are often providing it with the handle to an anonymous function. Anonymous functions have cost to invoke, so they are almost always slower than looping.
for i=1:height(ss)
There you are expecting height(ss) to return a scalar.
g=zeros(height(ss));
When you pass a scalar to zeros(), the result is a square array the given size, not a 1 x n or n x 1 array of zeros.
for i=1:height(ss)
z=find(ss(:,i)>0);
you are using i as the second index to ss. The default height function returns the number of rows of the given array, not the number of columns. So it is a mistake to be looping of the number of rows instead of the number of columns.
shamal
shamal el 13 de Jul. de 2023
thanks for the tips
shamal
shamal el 13 de Jul. de 2023
rik:" Another small tweak: you don't need find, since you can use ~any(z) and use z directly to index the result."
can you give me example? you write ~any(z) but i want >0
Rik
Rik el 14 de Jul. de 2023
I'm ignoring the comments Walter gave you to make it more clear what edits I mean:
%ss is array double
%profit is array double
g = zeros(height(ss));
maxCat = 3;
for i=1:height(ss)
z = ss(:,i)>0;
if ~any(z)
[val,idx] = sort(profit(z),'descend');
ret = idx(1:maxCat);
g(ret,i) = 1;
end
end
This will have the same effect as the code you posted.
shamal
shamal el 14 de Jul. de 2023
oky txk

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Más información sobre Matrices and Arrays en Centro de ayuda y File Exchange.

Preguntada:

el 13 de Jul. de 2023

Comentada:

el 14 de Jul. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by