need help in optimizing and figuring out what is wrong with the following code.
1 次查看(过去 30 天)
显示 更早的评论
The following code takes 2 matrices/arrays of 2xN dimension and then looks for those values in B which match with A along the row dimension.
Since there can be duplicates we only want to look at values that occur twice and not more than that.
First of all is the code correct
Secondly on executing the first version I am able to find the indices but the answer is not right.Reason being expected output should be 24 but actual output is 187.
Input
A matrix 2x329
B matrix 2x21
actual O/P 1x187
expected o/p 80 82 84 86 87 90 93 96 99 103 107 110 113 116 120 124 128 132 136 140 144 148
%code version 2. for duplicates
ids=[];
for i=1:size(A,2)
ctr=1;
for j=1:size(B,2)
if(A(i)==B(j) && ctr<=2)
ids=[ids,i];
ctr=ctr+1;
elseif ( A(i)==B(j) && ctr>2)
continue
else
continue
end
end
end
%code version1 doesn't handle duplicates
ids=[];
for i=1:size(A,2)
for j=1:size(B,2)
if(A(i)==B(j))
ids=[ids,i];
else
continue
end
end
end
0 个评论
回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!