Help me please: Problem to compare elements in array with its label using grp2idx and grpstats

3 views (last 30 days)
My problem is stated above.I have done trouble shooting and turned out these comparison is evaluated to false, thus I cannot count the occurences.
if((grp1(counting)== g1{ixx}) && (grp2(counting) == g2{ix}))
Is there any other way to compare them as I have tried using isequal function.Besides the coding seems logic to me.
grp1, grp2 are instances frm f1 and f2 respectively after using grp2idx function
g1,g2 are labels name frm f1,f2 respectively after using grpstats function.
e.g. the coding is to count occurences of instances of 1st label of g1 in grp1 given it is the 1st label of g2 in grp2;smthg to do with probability.
Thank you.
transportA data
1 10
1 11
0 11
0 10
1 11
1 10
0 11
0 11
1 12
0 12
load transportA
A=dataset(transportA);
[nobs,nvars] = getsize(A);
BB = A(:,1);
disp(BB);
grp1=grp2idx(BB);
disp('group1 = ');disp(grp1);
ct1=max(grp1);disp('qty grp1 = ');disp(ct1);
[g1,num1]=grpstats(grp2idx(BB),grp1,{'gname','numel'});
disp('g1 = ');disp(g1);
disp('num1 = ');disp(num1);
DD = A(:,2);
disp(DD);
grp2=grp2idx(DD);
disp('group2 = ');disp(grp2);
ct2=max(grp2);
disp('qty grp2 = ');disp(ct2);
[g2,num2]=grpstats(grp2idx(DD),grp2,{'gname','numel'});
disp('g2 = ');disp(g2);
disp('num2 = ');disp(num2);
for ix = 1 : ct2
for ixx = 1 : ct1
count = 0 ;
label1 = g1{ixx};
label2 = g2{ix};
disp('label1 = ');disp(label1);
disp('label2 = ');disp(label2);
for counting = 1 : nobs
oblong1 = grp1(counting);disp('oblong1 = ');disp(oblong1);
oblong2 = grp2(counting);disp('oblong2 = ');disp(oblong2);
%tf=isequal(grp1(counting),g1{ixx});disp('tf=');disp(tf);
if(grp1(counting)== g1{ixx}) && (grp2(counting) == g2{ix})
count = count + 1;
end
end
disp('count = ');disp(count);
end
end

Accepted Answer

Oleg Komarov
Oleg Komarov on 5 Aug 2011
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:
double('1')
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]])
  2 Comments
YASMIN M. Yacob
YASMIN M. Yacob on 8 Aug 2011
Thank you very much for the help. It works perfectly. I got confused with comparing labels of chars and instances of double....I better watch out for the data type that I am comparing next time. Thanks again.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!