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 commentaires

Rik
Rik le 13 Juil 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 le 13 Juil 2023
Modifié(e) : shamal le 13 Juil 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 le 13 Juil 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?
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 le 13 Juil 2023
thanks for the tips
shamal
shamal le 13 Juil 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 le 14 Juil 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 le 14 Juil 2023
oky txk

Connectez-vous pour commenter.

Réponses (0)

Catégories

En savoir plus sur Matrices and Arrays dans Centre d'aide et File Exchange

Question posée :

le 13 Juil 2023

Commenté :

le 14 Juil 2023

Community Treasure Hunt

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

Start Hunting!

Translated by