sparse matrix index shifting (a bug?)

If we run the following code:
la = sparse(ceil(5*rand(1,100)), 1:100, true, 5, 100);
lb = sparse(ones(1,20), 1:20, true, 1, 100);
lb = lb(randperm(100)); % if remove this line, everything is fine
ra = double(bsxfun(@and, la, lb));
disp(sum(sum(ra)));
rb = double(bsxfun(@and, la, full(lb)));
disp(sum(sum(rb)));
the output would be:
(1,1) 98
(1,1) 20
The outputs should be the same since the function full does not change the content of lb, but matlab gives different results in both version 2010a and 2011b (I have only tested in these two versions). It seems that the inconsistency comes from the index shifting operation in line 3. If line 3 is removed, everything becomes fine. Is there any explanation about this? or did I miss something?
Thank you very much.

 채택된 답변

Jan
Jan 2011년 9월 29일

1 개 추천

I assume it is a problem of BSXFUN with sparse logical arrays:
la = sparse(ceil(2 * rand(1, 10)), 1:10, true, 2, 10);
lb = sparse(ones(1, 5), 1:2:10, true, 1, 10);
r = double(la & lb(ones(1, 2), :));
disp(full(r)); % *Ok*
fprintf('\n');
r = double(bsxfun(@and, la, full(lb)));
disp(full(r)); % *Ok*
fprintf('\n');
r = double(bsxfun(@and, la, double(lb)));
disp(full(r)); % *Ok*
fprintf('\n');
r = double(bsxfun(@and, la, lb));
disp(full(r)); % *Fault*
I did not found this in the list of known bugs, but searching is not easy. Please contact the technical support.

추가 답변 (0개)

카테고리

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

질문:

2011년 9월 26일

Community Treasure Hunt

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

Start Hunting!

Translated by