"applicable automatic broadcast operation" on matrices of the same size

I am writing homework and I encountered this error using Octave. This does not affect the functionality of my solution, however, I am curious why this warning is 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 

Yours faithfully,
Tom

+6
source share
2 answers

Start the analysis with this:

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

The left factor is obviously numel(X) -by- 1 , and the right factor is 1 -by- p , and you get a matrix that is numel(X) -by- p .

Then here is the problem:

 X_poly = X .^ powers; 

The left operand is numel(X) -by- 1 , and the right operand is numel(X) -by- p . They do not match.

I think this is just a mistake, but the octave seems to be trying to understand what you meant, not what you wrote, but then gives you a warning that it guesses.

To do a one-time extension, you can use bsxfun or repmat .

 X_poly = bsxfun(@power, X, powers); X_poly = repmat(X, 1, p) .^ powers; 

Since assigning powers is just a messy way to do repmat , and bsxfun will extend both options, you can simply do:

 X_poly = bsxfun(@power, X, 1:p); 

which makes it somewhat pointless to have a special function for this purpose.

enter image description here

+3
source

Automatic broadcasting is a fairly new Octave feature that generates bsxfun wherever there is a measurement mismatch between single-point and non-single measurements.

In this case

X_poly = X. ^ powers;

replaced by

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

This is a completely legal octave behavior, and it seems that this is exactly what you want to do, so you do not need to change it at all.

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

Moreover, a common practice among many Octave programmers is to rely mainly on size mismatch as a way to detect errors in their program. I even once took a machine learning class, where a professor said to the whole class, "If all sizes match, then this is probably right." This is terrible, scary advice and a surefire way to make sure that everyone does not do their homework, but it reflects the general approach of many researchers to writing Matlab / Octave programs.

For this reason, introducing automatic broadcasting without any warning can cause trouble tracking if you are not in the habit of making explicit statements about your function entries.

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 compatibility with Matlab or just don't use automatic broadcasting and prefer to have an octave error to help isolate errors, you can add

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

instead.

+12
source

Source: https://habr.com/ru/post/952622/


All Articles