Accumarray strange behaviour when working on characters.

2 visualizaciones (últimos 30 días)
Jan Orwat
Jan Orwat el 3 de Nov. de 2014
Editada: Jan Orwat el 3 de Nov. de 2014
Hello, recently I've been doing some string operations using function accumarray. Problem noticed on MATLAB R2013a and R2010b on my university PC and on cody serwers (R2014b). I've tried to pass string as an val argument and use space character as fillval. The idea of an operation is presented by the code below:
subs = [2 1; 3 2; 2 3; 3 4; 2 5; 3 6];
val = 'MATLAB';
size = [4 6];
fun = @char;
fillval = '.';
A = accumarray(subs, val, size, fun, fillval);
with those parameters I expect this working like that:
A =
......
M.T.A.
.A.L.B
......
but I'm getting :
A =
⸮⸮⸮ 䀁
MT A
AL B
⸮⸮⸮
So the letters from val are placed correctly in array, but it looks like there are random signs put instead of fillval.
What I've got till now is:
  1. subs format doesn't affect result, works same with all formats shown in documentation,
  2. val is converted to numeric by accumarray, and its doesn't affect final results,
  3. same for size,
  4. fun can be @char or @(IN)char(min(IN)) or whatever. In my case, there is always one letter in one place, so it should not affect result. When it is left to default, result is numeric and has to be converted to characters (see p.2) and then accumarray works fine, without any side effects.
  5. fillval can be any char, but it doesn't work. When fun set to default, it has to be numeric and then no side effects occur, function works fine. When fun convert values to chars, and fillval is set to default also no side effects occur, empty spaces are filled with zeros.
Of course, I am able to obey this by simply stop using fillval:
A = accumarray(subs, val, size, fun);
A(~A) = fillval
A =
......
M.T.A.
.A.L.B
......
or work on integers and convert to strings but that's not the point. My goal is if there is a bug in accumarray or I've been doing something wrong and I am missing something?

Respuesta aceptada

Sean de Wolski
Sean de Wolski el 3 de Nov. de 2014
Editada: Sean de Wolski el 3 de Nov. de 2014
It looks like at some point there is a data type conversion and saturation occurs:
uint8(A)
ans =
255 255 255 0 0 0
77 255 84 255 65 0
255 65 255 76 159 66
255 255 255 0 0 0
It's certainly appears to be unexpected, I would contact tech support.
  1 comentario
Jan Orwat
Jan Orwat el 3 de Nov. de 2014
Editada: Jan Orwat el 3 de Nov. de 2014
Thanks for your answer. The weirdest thing is it look like beeing kind of random efect. Here is my output for couple of times of the same input:
>> +accumarray([2 1; 3 2; 2 3; 3 4; 2 5; 3 6],'MATLAB',[4 6],@char,'.')
ans =
11822 11822 11822 92 76 102
77 11822 84 77 65 108
11822 65 11822 76 66 66
11822 11822 11822 84 92 116
>> +accumarray([2 1; 3 2; 2 3; 3 4; 2 5; 3 6],'MATLAB',[4 6],@char,'.')
ans =
11822 11822 11822 0 16401 0
77 11822 84 0 65 0
11822 65 11822 76 0 66
11822 11822 11822 0 0 0
>> +accumarray([2 1; 3 2; 2 3; 3 4; 2 5; 3 6],'MATLAB',[4 6],@char,'.')
ans =
11822 11822 11822 0 0 0
77 11822 84 0 65 0
11822 65 11822 76 0 66
11822 11822 11822 0 0 0
>> +accumarray([2 1; 3 2; 2 3; 3 4; 2 5; 3 6],'MATLAB',[4 6],@char,'.')
ans =
11822 11822 11822 0 15 1
77 11822 84 0 65 10027
11822 65 11822 76 0 66
11822 11822 11822 0 0 112
11822/'.' is 257, and this 257 appears for other chars I've tried but I'm not sure if it leads anywhere.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Data Type Conversion en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by