bincy thankachan
Newbie level 6
I'm working with PSO algorithm. This is the code, i want the output to be added and give a value.
a=[2 3 7 1 4 2 1 2 8 2 5 9 2 10 12 1 2 6];
b=unique(a);
[b m n]=unique(a);
counts=accumarray(n),1)
for k=1:numel(counts)
fprintf('the value %d appears %d times in a\n',...
b(k),counts(k));
end
for i=1:counts
if(counts(i)==1)
disp('q=0');
end
if(counts(i)==2)
disp('r=0');
end
if(counts(i)==3)
disp('s=3');
end
if(counts(i)==4)
disp('t=6');
end
if(counts(i)==5)
disp('u=10');
end
if(counts(i)==6)
disp('v=15');
end
end
The output is coming like this:
counts =
3
6
1
1
1
1
1
1
1
1
1
the value 1 appears 3 times in a
the value 2 appears 6 times in a
the value 3 appears 1 times in a
the value 4 appears 1 times in a
the value 5 appears 1 times in a
the value 6 appears 1 times in a
the value 7 appears 1 times in a
the value 8 appears 1 times in a
the value 9 appears 1 times in a
the value 10 appears 1 times in a
the value 12 appears 1 times in a
s=3
v=15
q=0
These values should be added and give the output as 18. I'm new to matlab so pls help me out.
Thanks.
a=[2 3 7 1 4 2 1 2 8 2 5 9 2 10 12 1 2 6];
b=unique(a);
[b m n]=unique(a);
counts=accumarray(n),1)
for k=1:numel(counts)
fprintf('the value %d appears %d times in a\n',...
b(k),counts(k));
end
for i=1:counts
if(counts(i)==1)
disp('q=0');
end
if(counts(i)==2)
disp('r=0');
end
if(counts(i)==3)
disp('s=3');
end
if(counts(i)==4)
disp('t=6');
end
if(counts(i)==5)
disp('u=10');
end
if(counts(i)==6)
disp('v=15');
end
end
The output is coming like this:
counts =
3
6
1
1
1
1
1
1
1
1
1
the value 1 appears 3 times in a
the value 2 appears 6 times in a
the value 3 appears 1 times in a
the value 4 appears 1 times in a
the value 5 appears 1 times in a
the value 6 appears 1 times in a
the value 7 appears 1 times in a
the value 8 appears 1 times in a
the value 9 appears 1 times in a
the value 10 appears 1 times in a
the value 12 appears 1 times in a
s=3
v=15
q=0
These values should be added and give the output as 18. I'm new to matlab so pls help me out.
Thanks.