Accumarray strange behaviour when working on characters.
3 views (last 30 days)
Show older comments
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 =
⸮⸮⸮ 䀁
M⸮T A
⸮A⸮L 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:
- subs format doesn't affect result, works same with all formats shown in documentation,
- val is converted to numeric by accumarray, and its doesn't affect final results,
- same for size,
- 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.
- 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?
0 Comments
Accepted Answer
Sean de Wolski
on 3 Nov 2014
Edited: Sean de Wolski
on 3 Nov 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 Comment
More Answers (0)
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!