Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Change in Indexing for Sparse Matrix Input

Subject: Change in Indexing for Sparse Matrix Input

From: Brian Borchers

Date: 10 Nov, 2010 17:19:13

Message: 1 of 3

There's a seemingly innocuous change to MATLAB in R2010a that has some
surprising consequences:

>Change in Indexing for Sparse Matrix Input
>
>Now subscripted reference into a sparse matrix always returns a sparse
>matrix. In previous versions of MATLAB, using a double scalar to index
>into a sparse matrix resulted in full scalar output.

Consider the following example:

>> c=sparse([1; 2; 0; 0; 5]);
>> fprintf(1,'c(5) is %f\n',c(5))
??? Error using ==> fprintf
Function is not defined for sparse inputs.

There's a relatively simple workaround:
 >> fprintf(1,'c(5) is %f\n',full(c(5)))
c(5) is 5.000000

However, it's a pain to go back and modify every single fprintf() call
in my code to use the "full()" wrapper around the arguments to
fprintf, not to mention dealing with all of the bug reports from
people who use my code and find that this change has broken it.

Can anyone suggest any alternative to this "full()" work around?

Subject: Change in Indexing for Sparse Matrix Input

From: Matt J

Date: 10 Nov, 2010 19:24:05

Message: 2 of 3

Brian Borchers <borchers.brian@gmail.com> wrote in message <e2b7157b-46e1-4b73-96ae-bc68c769a839@a9g2000pro.googlegroups.com>...
>
> However, it's a pain to go back and modify every single fprintf() call
> in my code to use the "full()" wrapper around the arguments to
> fprintf, not to mention dealing with all of the bug reports from
> people who use my code and find that this change has broken it.
>
> Can anyone suggest any alternative to this "full()" work around?
=========

You could write your own fprintf.m, such as the one below, as a wrapper to the builtin fprintf which gets invoked via BUILTIN

function out=fprintf(varargin)

    varargin(3:end)=cellfun(@full,varargin(3:end),'uni',0);

    if nargout
     out=builtin('fprintf',varargin{:});
    else
      builtin('fprintf',varargin{:});
    end

end

It might be worth putting this in a private/ directory for the code that you distribute.

Subject: Change in Indexing for Sparse Matrix Input

From: James Tursa

Date: 10 Nov, 2010 20:45:07

Message: 3 of 3

"Matt J " <mattjacREMOVE@THISieee.spam> wrote in message <ibergl$abb$1@fred.mathworks.com>...
> Brian Borchers <borchers.brian@gmail.com> wrote in message <e2b7157b-46e1-4b73-96ae-bc68c769a839@a9g2000pro.googlegroups.com>...
> >
> > However, it's a pain to go back and modify every single fprintf() call
> > in my code to use the "full()" wrapper around the arguments to
> > fprintf, not to mention dealing with all of the bug reports from
> > people who use my code and find that this change has broken it.
> >
> > Can anyone suggest any alternative to this "full()" work around?
> =========
>
> You could write your own fprintf.m, such as the one below, as a wrapper to the builtin fprintf which gets invoked via BUILTIN
>
> function out=fprintf(varargin)
>
> varargin(3:end)=cellfun(@full,varargin(3:end),'uni',0);
>
> if nargout
> out=builtin('fprintf',varargin{:});
> else
> builtin('fprintf',varargin{:});
> end
>
> end
>

A slight syntax variation of the above I typically use for overriding built-in functions is:

function varargout = fprintf(varargin)
% adjust input arguments here
for k=1:nargin
    if( issparse(varargin{k} )
        varargin{k} = full(varargin{k});
    end
end
% call the built-in function
[varargout{1:nargout}] = builtin('fprintf',varargin{:});
end


James Tursa

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us