g1{ixx} is a char and you are comparing it with a double.
When compared to a numeric class a char is converted to its ASCII numerical value:
So, the correct comparison should be carried out as:
if grp1(counting) == str2double(g1{ixx}) && grp2(counting) == str2double(g2{ix})
--------------------------------------------------------------------------------------------------------------------------------------------
In the end this is the polished code I came up:
transportA = [1 10;1 11;0 11;0 10;1 11;1 10;0 11;0 11;1 12;0 12];
A = dataset({transportA,'trasnportA','data'});
nobs = 10;
grp1 = grp2idx(A.trasnportA);
ct1 = max(grp1);
[g1,num1] = grpstats(grp1,grp1,{'gname','numel'});
grp2 = grp2idx(A.data);
ct2 = max(grp2);
[g2,num2] = grpstats(grp2,grp2,{'gname','numel'});
for ix = 1 : ct2
for ixx = 1 : ct1
count = 0 ;
label1 = g1{ixx};
label2 = g2{ix};
for counting = 1 : nobs
oblong1 = grp1(counting);
oblong2 = grp2(counting);
if oblong1 == str2double(g1{ixx}) && oblong2 == str2double(g2{ix})
count = count + 1;
end
end
disp('count = ');disp(count);
end
end
--------------------------------------------------------------------------------------------------------------------------------------------
But what you're really doing is accomplished by crosstab: transportA = [1 10;1 11;0 11;0 10;1 11;1 10;0 11;0 11;1 12;0 12];
A = dataset({transportA,'trasnportA','data'});
tmp = crosstab(A.trasnportA,A.data)
% Print on screen
fprintf('%5d%5d%5d%5d\n',[[NaN unique(A.data).'];[unique(A.trasnportA) tab]])
Best Answer