필터 지우기
필터 지우기

How to compare rows of an array with other rows?

조회 수: 12 (최근 30일)
lucksBi
lucksBi 2018년 1월 19일
댓글: lucksBi 2018년 1월 24일
Hey.. How to compare rows of an array with rows defined in x?
array={[1,2,3,4,6,13,18,9];[13];[12,1,2,5];[3,4];[1,5,6]}
x=[1;3]
comparison will be based on x. e.g. first element in x is 1 so it will compare 1st row of array with all other rows. 2nd element is 3 so 3rd row will be compared will all other rows. Comparison will get intersection of elements like:
result{1,1}= {[13;[1,2];[3,4];[1,6]]}
result{2,1}= {[[1,2];0;0;[1,6];[1,5]]}
0 means no common element was found.
please help.
  댓글 수: 4
Jan
Jan 2018년 1월 19일
편집: Jan 2018년 1월 19일
The question is not clear. The elements of the cell array called "array" have one row only. Do you mean, that their first columns are compared? Or do you mean the 1st and 3rd element of the cell array?
Note that there is a missing ] in
result{1,1}= {[13;[1,2];[3,4];[1,6]]}
and this is not valid also:
result{2,1}= {[[1,2];0;0;[1,6];[1,5]]}
because you cannot concatenate vectors of different width vertically.
Jos (10584)
Jos (10584) 2018년 1월 19일
and you do realise that you only have to process the unique values in x?

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

채택된 답변

Jan
Jan 2018년 1월 19일
편집: Jan 2018년 1월 19일
The question is not clear, but I guess a code which produces the output:
array = {[1,2,3,4,6,13,18,9];[13];[12,1,2,5];[3,4];[1,5,6]}
x = [1;3];
Result = cell(1, numel(x));
n = numel(array);
for ix = 1:numel(x)
index = x(ix);
A = array{index};
B = cell(1, n - 1);
iB = 0;
for ia = 1:n
if ia ~= index
iB = iB + 1;
Inter = intersect(A, array{ia});
if isempty(Inter)
Inter = 0;
end
B{iB} = Inter;
end
end
Result{ix} = B;
end
It might help to save time, if you sort the arrays at first:
for k = 1:numel(array)
array{k} = sort(array{k});
end
  댓글 수: 4
lucksBi
lucksBi 2018년 1월 24일
편집: lucksBi 2018년 1월 24일
ThankYou so much. Working good on smaller matrix but It is showing following error when i run on actual dataset. Can you please help on his i have tried almost everything i know to resolve this but still not successful:
Index exceeds matrix dimensions.
Error in test2 (line 54)
ai = unique(array{x(i)})
lucksBi
lucksBi 2018년 1월 24일
Problem solved. thanks alot for your time.

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

추가 답변 (1개)

Birdman
Birdman 2018년 1월 19일
편집: Birdman 2018년 1월 19일
Another approach for your problem, which is shorter:
for i=1:numel(x)
temp=setdiff(1:size(array,1),x(i));
for j=1:numel(temp)
result{j,i}=intersect(array{x(i),1},array{temp(j),1});
end
end
  댓글 수: 2
Jan
Jan 2018년 1월 19일
편집: Jan 2018년 1월 19일
Yes, it is shorter. But it omits the pre-allocation of result and using 0 as output, if no intersections are found. It is slower, because array{x(i),1} is addressed in each iteration.
Another idea, which avoids setdiff, which is an overkill for a scalar input:
n = size(array, 1);
v = 1:n;
result = cell(n-1, numel(x));
result(:) = {0};
for i = 1:numel(x)
temp = v(v ~= i); % Short form of: setdiff(v, i)
ai = array{x(i)}; % Or maybe: ai = unique(array{x(i)})
for j = 1:n-1
r = intersect(ai, array{temp(j)});
if ~isempty(r)
result{j,i} = r;
end
end
end
This might be an efficient mix of our solutions.
lucksBi
lucksBi 2018년 1월 20일
Thank You so much for your time. this is really helpful.

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

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by