%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

Rik
Rik 2023년 7월 13일
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 2023년 7월 13일
편집: shamal 2023년 7월 13일
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 2023년 7월 13일
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 2023년 7월 13일
thanks for the tips
shamal
shamal 2023년 7월 13일
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 2023년 7월 14일
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 2023년 7월 14일
oky txk

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

도움말 센터File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

질문:

2023년 7월 13일

댓글:

2023년 7월 14일

Community Treasure Hunt

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

Start Hunting!

Translated by