matlab double values saved into a cell as integers

8 views (last 30 days)
Hello, i have being coding run length encoder on matlab and it worked perfectly fine, but when i try a matrix of double values they got saved into my cell as rounded integers and i need them to stay as double integers, how can i do that ?
My code:
clear all;
%***reading a matrix x from the directory c:\
[map,x]=imread(['c:\mAT\07.tif']);
x(112,2)
f=size(x);
c=1;
h=uint16(c);
y={};
double(y);
for i=1:f(1)
y{i}=[];
for j=1:f(2)-1
if x(i,j)==x(i,j+1)
h = h + 1;
else
y{i}=[y{i} h x(i,j)];
h=uint16(c);
end
end
y{i} =[y{i} h x(i,f(2))];
h=uint16(c);
end
save('map1.txt','x')
save('map2.mat','y')
k=size(y);
z={};
for s=1:k(2)
a=size(y{1,s});
L=1;
z{s}=[];
while(L<a(2))
for n=1:y{1,s}(1,L);
z{s}=[z{s} y{1,s}(1,L+1)];
end
L = L+2;
end
end
new_z=cell2mat(z(:));
imshow(map,x)

Answers (1)

Walter Roberson
Walter Roberson on 5 Mar 2022
h=uint16(c);
That is a uint16
h = h + 1;
Regardless of the actual implementation, with h being uint16, that is defined to happen "as-if" the code were
h = uint16( double(h) + 1 )
so even though that code uses double precision 1, the end result will be uint16.
y{i}=[y{i} h x(i,j)];
The only way that line can be reached is through paths in which h is uint16. y{i} starts out empty, so the first time that statement would be
y{i} = [[] h x(i,j)]
which would be
y{i} = horzcat([], h, x(i,j))
When you have [] in a horzcat or vertcat context, and at least one other entry is non-empty, then the [] is effectively silently dropped from the list, and the result is the same as-if the [] were not present -- in that one specific context of [] the double datatype of [] is ignored, and the datatype of the result is whatever is the result of concatenating together the other arguments. Thus, the result for the first pass would be the same as if you had had y{i} = [h x(i,j)] .
Now, h is uint16, and x looks like it is part of a colormap and so is probably a floating point number in the range 0 to 1 (inclusive). So you are [] between a uint16 and a double.
What datatype results when you [] together a uint16 and a double? Well, refer to https://www.mathworks.com/help/matlab/matlab_prog/valid-combinations-of-unlike-classes.html Row "integer" combines with (column) double to give... integer.
When you [] a uint16 and a double, the double is converted by way of uint16() to form a uint16 that is concatenated on to the other uint16 .
You cannot preserve mixed datatypes when using [] between two numeric objects. One of the numeric objects will be converted to the other one; the general rule is that the result is the "most restrictive type" out of the combination of types. (The flow will still proceed left to right, so values might potentially be converted several times; MATLAB does not scan all of the entries first to determine the final class and immediately convert to that class.
If you need the double precision value to be preserved, you will either need to double(h) in that context, or else you will need to use a cell array.

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!