Out of Memory using bsxfun MATLAB

2019-08-17 18:07发布

I try to implement image compression using Burrows-Wheeler transform. Consider an 1D matrix from path scanning is:

p = [2 5 4 2 3 1 5];

and then apply the Burrows-Wheeler transform :

function output = bwtenc(p) 
n = numel(p);
x = zeros(length(p),1);
for i = 1:length(p)
    left_cyclic = mod(bsxfun(@plus, 1:n, (0:n-1).')-1, n) + 1;
    x = p(left_cyclic);
end
[lex ind] = sortrows(x);
output = lex(:,end);
output = uint8(output(:)');
end

And it works! But the problem is when i try to implement 1D matrix from Lena.bmp which the size is 512*512, Error message showing that bsxfun is out of memory. Anyone please help me.

1条回答
时光不老,我们不散
2楼-- · 2019-08-17 18:32

See if this works for you -

function output = bwtenc(p) 
np = numel(p);
[~,sorted_ind] = sort(p);
ind1 = mod((1:np)+np-2,np)+1;
output = p(ind1(sorted_ind));
output = uint8(output(:)');
end
查看更多
登录 后发表回答