필터 지우기
필터 지우기

Faster Rank Calculations and Indexing - For Loop or Another Way?

조회 수: 2 (최근 30일)
Shawn Cooper
Shawn Cooper 2020년 6월 21일
댓글: Shawn Cooper 2020년 6월 23일
Hello, I'm using the following code to find the rank of all nxn matrices with matrix entries [0 1] and plot them in a histogram.
N = 4;
n2 = N^2;
x = repmat({[0, 1]}, 1, n2);
M = reshape(combvec(x{:}), 4, 4, []);
r = [];
for i = [1:length(M)]
r = [r rank(M(:,:,i))];
end
hist(r)
As additional possble matrix entries are added ([0 1 2], for example), the number of possible matrices increases exponentially and the speed of the rank() function decreases.
Is there a faster way to do this?
  댓글 수: 2
Walter Roberson
Walter Roberson 2020년 6월 22일
Growing the r array dynamically is really killing performance for you.
Shawn Cooper
Shawn Cooper 2020년 6월 23일
Thanks for the feedback on what the issue was. I appreciate it!

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

채택된 답변

Walter Roberson
Walter Roberson 2020년 6월 22일
N = 4;
n2 = N^2;
ents = [0 1 2];
tic
[parts{1:n2}] = ndgrid(ents);
toc
tic
pages = reshape(cell2mat(cellfun(@(C)C(:).', parts(:), 'uniform', 0)), N, N, []);
toc
np = size(pages,3);
ranks = zeros(1, np);
tic
for K = 1 : np
ranks(K) = rank(pages(:,:,K));
end
toc

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Interpolation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by