Skip to content

Commit

Permalink
Patched bwselect
Browse files Browse the repository at this point in the history
It gives different results from Matlab, at least in Octave's Image Package 2.12.0
  • Loading branch information
lmendo committed Sep 22, 2022
1 parent 1a96b88 commit a7e1650
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# MATL
A programming language based on MATLAB/Octave and suitable for code golf.

The compiler works in MATLAB R2015b or newer. Probably in older versions too, except for some specific functions. It is also compatible with Octave 4.0.0. The compiler tries to ensure consistent behaviour in both platforms. In addition, you can use it at [Try it online!](https://tio.run/#matl) and at [MATL Online](https://matl.io).
The compiler works in MATLAB R2015b or newer. Probably in older versions too, except for some specific functions. It is also compatible with Octave 4.0.0. The compiler tries to ensure consistent behaviour in both platforms. In addition, you can use it at [Try it online!](https://tio.run/#matl) and at [MATL Online](https://matl.suever.net/).

Installation: unpack the compressed file to a folder, and make that folder part of MATLAB's or Octave's search path.

Expand Down
15 changes: 15 additions & 0 deletions compatibility/bwselect_comp.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
function BW2 = bwselect_comp(BW1, C, R, N)
% bwselect in some versions of Octave's Image Package (for example 2.12.0) gives different results
% from those in Matlab. This fixes that. The approach used here is: find the connected components
% and select those that contain any of the input pixels
CC = bwconncomp(BW1, N);
PixelIdxList = CC.PixelIdxList;
clear CC
lin_ind = sub2ind(size(BW1), R(:), C(:));
BW2 = false(size(BW1));
for c = 1:numel(PixelIdxList)
if any(ismember(lin_ind, PixelIdxList{c}))
BW2(PixelIdxList{c}) = true;
end
end
end
Binary file modified funDef.mat
Binary file not shown.
2 changes: 1 addition & 1 deletion funDef.txt
Original file line number Diff line number Diff line change
Expand Up @@ -898,7 +898,7 @@ YI 3 4 3 4 1 1 1 true true true true str = {'distinct' 'sliding'}; rearrange ma
if isscalar(in{3}), in{3}(2) = numel(in{1})/in{3}; end
if prod(in{3})~=numel(in{1}), in{3} = round(in{3}*sqrt(numel(in{1})/prod(in{3}))); end
out{1} = col2im(in{:});
ZI 1 inf 2 3 1 inf 1 true true true true switch in{end} image processing functions Depending on numeric last input, calls an image processing function with the remaining inputs. $0$: \matlab+imfill+. If first input is logical or numerical it is converted to char. $1$: \matlab+bwlabeln+. $2$: \matlab+imdilate+. This function allows second input to be number $4$, $5$, $8$ or $9$, which is interpreted as the corresponding neighbourhood mask. $3$: \matlab+imerode+. This function allows second input to be number $4$, $5$, $8$ or $9$, which is interpreted as the corresponding neighbourhood mask. $4$: \matlab+bweuler+. $5$: \matlab+bwselect+ with $4$ inputs.
ZI 1 inf 2 3 1 inf 1 true true true true switch in{end} image processing functions Depending on numeric last input, calls an image processing function with the remaining inputs. $0$: \matlab+imfill+. If first input is logical or numerical it is converted to char. $1$: \matlab+bwlabeln+. $2$: \matlab+imdilate+. This function allows second input to be number $4$, $5$, $8$ or $9$, which is interpreted as the corresponding neighbourhood mask. $3$: \matlab+imerode+. This function allows second input to be number $4$, $5$, $8$ or $9$, which is interpreted as the corresponding neighbourhood mask. $4$: \matlab+bweuler+. $5$: \matlab+bwselect+ with $4$ inputs and $1$ output.
case 0
if islogical(in{1}) || ischar(in{1}), in{1} = double(in{1}); end
[out{:}] = imfill(in{1:end-1});
Expand Down
Binary file modified help.mat
Binary file not shown.
2 changes: 1 addition & 1 deletion matl_compile.m
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@
'triu' 'tril' 'randsample' 'nchoosek' 'vpa' 'sum' 'mean' 'prod' 'diff' 'mod' 'repelem' 'dec2bin' 'dec2base' ...
'hypergeom' 'disp' 'str2func' 'logical' 'circshift' 'pdist2' 'strsplit' 'max' 'min' 'strncmp' 'round'...
'datestr' 'regexp' 'regexprep' 'imshow' 'mat2str' 'blkdiag' 'strcat' 'str2num' 'str2double' 'cconv' ...
'gcd' 'lcm' 'fftn' 'mode' 'nnz' 'str2sym' 'factor' 'bwlabeln' 'perms'};
'gcd' 'lcm' 'fftn' 'mode' 'nnz' 'str2sym' 'factor' 'bwlabeln' 'perms' 'bwselect'};
verNumTh = [4 0 0]; % first version in which a modified function is not needed:
if (verNum(1)<verNumTh(1)) || ((verNum(1)==verNumTh(1)) && (verNum(2)<verNumTh(2))) || ((verNum(1)==verNumTh(1)) && (verNum(2)==verNumTh(2)) && (verNum(3)<verNumTh(3)))
fnames = [fnames {'colon'}];
Expand Down
Binary file modified spec/MATL_spec.pdf
Binary file not shown.
2 changes: 2 additions & 0 deletions spec/MATL_spec.tex
Original file line number Diff line number Diff line change
Expand Up @@ -1445,6 +1445,8 @@ \subsection{MATL functions}
This has been fixed.
\item
\matlab+perms+ with an input array with more than one singleton dimension gives a different result in Octave than in MATLAB (where the result corresponds to linearizing the input). This has been fixed.
\item
\matlab+bwselect+ in some versions of Octave's Image Package (for example 2.12.0) gives different results from those in Matlab. This has been fixed.
\end{itemize}


Expand Down
2 changes: 1 addition & 1 deletion spec/funDefTable/funDefTable.tex
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@
\matl{I} & 0 & 0-- ($^\dagger$) & paste from clipboard I \\
\matl{XI} & 0-- (1) & 0 & copy to clipboard I \\
\matl{YI} & 3--4 (3 / 4) & 1 & \matlab+col2im+. Uses \matlab+'distinct'+ option by default. Second and third inputs may be scalars, and then they are interpreted as numbers of columns. Third input may be a two-vector with product less then the number of elements of first input, and then it is appropriately scaled. This function allows flag strings in fourth input to be replaced by numbers, as follows: 1: \matlab+'distinct'+, 2: \matlab+'sliding'+ \\
\matl{ZI} & 1-- (2 / 3) & 1-- (1) & Depending on numeric last input, calls an image processing function with the remaining inputs. $0$: \matlab+imfill+. If first input is logical or numerical it is converted to char. $1$: \matlab+bwlabeln+. $2$: \matlab+imdilate+. This function allows second input to be number $4$, $5$, $8$ or $9$, which is interpreted as the corresponding neighbourhood mask. $3$: \matlab+imerode+. This function allows second input to be number $4$, $5$, $8$ or $9$, which is interpreted as the corresponding neighbourhood mask. $4$: \matlab+bweuler+. $5$: \matlab+bwselect+ with $4$ inputs. \\
\matl{ZI} & 1-- (2 / 3) & 1-- (1) & Depending on numeric last input, calls an image processing function with the remaining inputs. $0$: \matlab+imfill+. If first input is logical or numerical it is converted to char. $1$: \matlab+bwlabeln+. $2$: \matlab+imdilate+. This function allows second input to be number $4$, $5$, $8$ or $9$, which is interpreted as the corresponding neighbourhood mask. $3$: \matlab+imerode+. This function allows second input to be number $4$, $5$, $8$ or $9$, which is interpreted as the corresponding neighbourhood mask. $4$: \matlab+bweuler+. $5$: \matlab+bwselect+ with $4$ inputs and $1$ output. \\
\matl{J} & 0 & 0-- ($^\dagger$) & paste from clipboard J \\
\matl{XJ} & 0-- (1) & 0 & copy to clipboard J \\
\matl{ZJ} & 1-- (2 / 3) & 0-- (1) & Depending on numeric last input, calls a symbolic-specific function with the remaining inputs. $0$: \matlab+simplify+. $1$: \matlab+pretty+. $2$: \matlab+latex+. $3$: \matlab+numden+. \sa \matl{X\$} \\
Expand Down

0 comments on commit a7e1650

Please sign in to comment.