problem having reading the IP address in sorting

조회 수: 8 (최근 30일)
Karan
Karan 2012년 4월 17일
i asked a question here and sir andrei bobrov came out with a good solution here http://www.mathworks.in/matlabcentral/answers/34805-sorting-and-counting
a = [ 1 2 5 7] ; b = [ 9 9 3 4] ;
[aa bb] = ndgrid(a,b);
k = [aa(:) bb(:)];
[n1, n2, n2] = unique(k,'rows');
n3 = histc(n2,1:max(n2));
out = [n1, n3]
but what i ask is if i have to input a cell with values as
['172.20.1.1' '172.20.0.31' '172.20.114.29']
and perform the same program i am getting error as
??? Undefined function or method 'full' for input arguments of type 'cell'.
Error in ==> ndgrid at 38
varargin{i} = full(varargin{i}); % Make sure everything is full
Error in ==> bob at 3
[aa bb] = ndgrid(a,b);
any solutions please ??

채택된 답변

Andrei Bobrov
Andrei Bobrov 2012년 4월 18일
a = { '172.20.113.214'
'85.17.72.66'
'172.20.113.214'
'38.124.168.125'
'46.45.178.252'
'172.20.113.214'
'46.45.178.252'
'172.20.113.214'
'172.20.113.1'
'46.45.178.252'
'172.20.113.214'
'46.45.178.252'
'172.20.113.214'
'46.45.178.252'
'172.20.113.214'
'38.124.168.125'
'172.20.113.214'
'38.113.165.77'
'172.20.113.214'
'38.124.168.125'
'172.20.113.214'
'95.211.85.42'
'172.20.113.214'
'38.113.165.77'
'172.20.113.214'}
[b n n] = unique(a);
out0 = accumarray(n,ones(numel(a),1));
out = [b, num2cell(out0)]
EDIT on comment
[A na na] = unique(a);
[B nb nb] = unique(b);
[av bv] = ndgrid(na,nb);
[v,nab,nab] = unique([av(:) bv(:)],'rows');
k = accumarray(nab,ones(numel(nab),1));
out = [A(v(:,1)) B(v(:,2)) num2cell(k)];
  댓글 수: 2
Karan
Karan 2012년 4월 18일
i though i have been misinterpreted by you sir , b is already given similar to a , check the pastie for the sample values of both a and b
sorry for less of info in last question
http://pastie.org/3809507
Karan
Karan 2012년 4월 18일
hey andrei, i screwed myself , i got my problem statement wrong and i will have to get back ..thanx for the solution

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Walter Roberson
Walter Roberson 2012년 4월 17일
You do not show us what your "a" and "b" are for this purpose, but you cannot ndgrid a cell array. You might want to look at bsxfun(). On the other hand, it looks to me as if you should probably be using accumarray()
  댓글 수: 1
Karan
Karan 2012년 4월 18일
take a = ['192.168.1.1' '192.168.1.2' '192.168.1.3' ... ] and b also similar
look for http://pastie.org/3809096

댓글을 달려면 로그인하십시오.


Jan
Jan 2012년 4월 18일
I guess, that you do not have:
a = ['172.20.1.1' '172.20.0.31' '172.20.114.29']
but
a = {'172.20.1.1' '172.20.0.31' '172.20.114.29'}
Andrei's solution works for number only. Therefore you could convert the string into numbers:
ac = {'172.20.1.1' '172.20.0.31' '172.20.114.29'}
a = zeros(size(ac));
for i = 1:numel(ac)
d = sscanf(ac{i}, '%d.%d.%d.%d');
a(i) = [16777216, 65536, 256, 1] * d;
end
The same for b.
  댓글 수: 2
Karan
Karan 2012년 4월 18일
yeah , thanx for the solution , but i want to continue with the IP address itself :)
Jan
Jan 2012년 4월 18일
The contents of [a] *are* the IP address, but in binary form. The conversion back to strings after processing works equivalenty.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by