Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'automatic broadcasting operation applied' on equally sized matrices

Tags:

octave

I'm writing a homework and I run into this error using Octave. It does not affect functionality of my solution, however I'm curious why this warning is being emitted.

% X is column vector, p is max degree of polynom
% example:
% X = [1;2;3;4], p = 3
% X_poly = [1,1,1; 2,4,8; 3,9,27; 4,16,64]
function [X_poly] = polyFeatures(X, p)

powers = ones(numel(X),1) * linspace(1,p,p);
X_poly = X .^ powers;

end

Regards,
Tom

like image 670
Tomas Zaoral Avatar asked Aug 27 '13 21:08

Tomas Zaoral


1 Answers

Automatic broadcasting is a fairly new Octave feature which throws in a bsxfun wherever there's a dimension mismatch between a singleton and non-singleton dimension.

In this case

X_poly = X .^ powers;

is replaced with

X_poly = bsxfun(@power, X, powers);

This is perfectly legal octave behavior and further it appears to be exactly what you want to do, so you don't have to change it at all.

The warning is because Matlab does not support automatic broadcasting so they want to remind you if you tried to run this code in Matlab it would fail.

Futhermore, common practice among many Octave programmers is to rely primarily on size mismatch as a way to detect bugs in their program. I even once took a machine learning class where the prof said to the whole class "If all the dimensions line up, then it's probably correct". This is terrible, terrible advice and a sure way to make sure everyone fails the homework assignments, but it does reflect a common approach among many researchers to writing Matlab/Octave programs.

For this reason, the introduction of automatic broadcasting without any warning could cause bug-tracing difficulties for you if you're not in the habit of making explicit assertions about your function inputs.

If you want to get rid of the warning, you can simply add

warning("off", "Octave:broadcast");

to your code.

If you want to maintain better Matlab compatibility or just don't use automatic broadcasting and would rather have octave error to help isolate bugs, you can add

warning ("error", "Octave:broadcast");

instead.

like image 135
dspyz Avatar answered Sep 28 '22 16:09

dspyz