Mercurial > octave
changeset 31849:a098cc74d9a5
dec2hex.m: Replace loop with if and transpose (bug #63833)
dec2hex.m: Replace for-loop with an if to detect an edge case,
and convert to column vector if so.
author | Arun Giridhar <arungiridhar@gmail.com> |
---|---|
date | Fri, 24 Feb 2023 14:12:47 -0500 |
parents | 1f3f7e874203 |
children | 43d56bbe9d40 |
files | scripts/strings/dec2hex.m |
diffstat | 1 files changed, 9 insertions(+), 11 deletions(-) [+] |
line wrap: on
line diff
--- a/scripts/strings/dec2hex.m Fri Feb 24 12:45:21 2023 -0500 +++ b/scripts/strings/dec2hex.m Fri Feb 24 14:12:47 2023 -0500 @@ -88,16 +88,14 @@ d = d(:, 1:4:end) * 8 + d(:, 2:4:end) * 4 + d(:, 3:4:end) * 2 + d(:, 4:4:end); ## Elements of d are now in the range 0 to 15. - ## Convert to char matrix and return. - ## We used to return this in a single line: - ## hstr = ("0123456789ABCDEF")(d+1); - ## But there are edge cases governing the sizes of row and column vectors - ## that cause problems with output size, so we use a loop instead. - hstr = repmat (' ', size (d)); - v = "0123456789ABCDEF"; - for t = 0:15 - hstr(d == t) = v(t + 1); - endfor + hstr = "0123456789ABCDEF"(d+1); + if (rows (hstr) < rows (d)) # this edge case happens when + hstr = hstr(:); # passing multiple inputs in the range 0 to 15. + ## If we don't manually convert it to column, we'd get all those + ## hex digits on the same line as one big string instead of one per line. + ## Good test for this: dec2hex (0:15) + ## compared with: dec2hex (uint64 (81985529216486895), 16) + endif endfunction @@ -124,7 +122,7 @@ %!assert (dec2hex ([1, 2; 3, -4]), ["01"; "03"; "02"; "FC"]) %!assert (dec2hex ({1, 2; 3, -4}), ["01"; "03"; "02"; "FC"]) -## Test that the output is of the correct size. +## Test that the output is of the correct shape. ## Next line should return a column vector: %!assert (dec2hex (0:15), "0123456789ABCDEF"(:)) ## Next line should return a row vector: