[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: bsxfun and sparse matrices
From: |
Luca Citi |
Subject: |
Re: bsxfun and sparse matrices |
Date: |
Sat, 19 May 2012 20:43:37 -0400 |
Hello Jordi,
I just found an old thread of yours asking about sparsity of the
results of bsxfun in Matlab. Below you can find some more information.
Basically:
- plus, minus, hypot, "or", "xor" return sparse only if both inputs are sparse;
- times, max, min, the comparisons, "and" return sparse if any input is sparse;
- rdivide, power, rem, mod, atan2 return sparse if the first is sparse;
- ldivide returns sparse if the second is sparse.
Everything makes sense (maybe with the exception of rdivide and
ldivide when both inputs are sparse). Please, also note that these
rules are not specific to bsxfun but apply to the normal operatons
between sparse matrices (see below).
Best,
Luca
>> x = sprand(1,3,1/3); y = sprand(3,1,1/3);
>> ops = address@hidden @minus @times @rdivide @ldivide @power @max; @min @rem
>> @mod @atan2 @hypot @eq @ne; @lt @le @gt @ge @and @or @xor};
>> cellfun(@(op) issparse(bsxfun(op, x, y)), ops)
ans =
1 1 1 1 1 1 1
1 1 1 1 1 1 1
1 1 1 1 1 1 1
>> cellfun(@(op) issparse(bsxfun(op, full(x), y)), ops)
ans =
0 0 1 0 1 0 1
1 0 0 0 0 1 1
1 1 1 1 1 0 0
>> cellfun(@(op) issparse(bsxfun(op, x, full(y))), ops)
ans =
0 0 1 1 0 1 1
1 1 1 1 0 1 1
1 1 1 1 1 0 0
>> isequal(cellfun(@(op) issparse(op(x, y')), ops), cellfun(@(op)
>> issparse(bsxfun(op, x, y)), ops))
ans =
1
>> isequal(cellfun(@(op) issparse(op(full(x), y')), ops), cellfun(@(op)
>> issparse(bsxfun(op, full(x), y)), ops))
ans =
1
>> isequal(cellfun(@(op) issparse(op(x, full(y'))), ops), cellfun(@(op)
>> issparse(bsxfun(op, x, full(y))), ops))
ans =
1