iAmWanteD
iAmWanteD

Reputation: 303

Comparing arrays of different sizes without any loop

The question goes like this:

Given two arrays, a & b (both with positive integers).

A special number is a number which a(i) == i (the value equals to the index).

How can you check if array b contains a value which is a special number of a.

For example: a = [9 9 3 9], b = [3 4 5]. Output will be 3. If b or a are empty, output is 0. If b contains several special number, only the smallest one will be shown.

This is what I have managed to do by far, can't progress from here..

a = input('Please enter the array a : ');
b = input('Please enter the array b : ');

indexedArray = 1:length(a);
c = a-indexedArray;
t = find(c==0);   
p = find(t==b);

does not work.

BTW: Can only use these functions: . sort , isempty , all , any , find , sum , max , min , length. No loops or conditions! Allowed only to use an array. No matrix. Cannot use logical operators such as &, |

Thanks!

Upvotes: 1

Views: 282

Answers (2)

jub0bs
jub0bs

Reputation: 66193

I was holding off posting my solution because I correctly suspected that this question was a homework assignment. However, since the OP has accepted Jonas's answer, I might as well post mine.

Code

A combination of sum, length, any, and min does the trick:

function out = stupidTutor(a, b)

a        = sum(a, 1);           % if a is empty, replace it by a 1-by-0 matrix
specials = a(a == 1:length(a)); % construct the vector of special numbers
b        = sum(b, 1);           % if b is empty, replace it by a 1-by-0 matrix

% some dyadic-product shenanigans
A   = specials' * (b == b);
B   = (specials == specials)' * b;
ind = any(A == B, 1);

temp = min(b(ind));         % temp is either a scalar, a 1-by-0 matrix, or []
out  = sum(sum(temp, 2), 1); % trick to return 0 in case temp be 1-by-0 or []

Tests

%               a           b                 result
stupidTutor([9 9 3 9]  , [3 4 5])       %       3  
stupidTutor([9 9 3 9]  , [9 8])         %       0
stupidTutor([9 9 9 9 5], [3 4 5 3])     %       5
stupidTutor([9 9 3 9 5], [3 4 5 3])     %       3
stupidTutor([9 9 3 9 5], [5 4 3 2 1])   %       3
stupidTutor([9 9 3 9]  , [])            %       0
stupidTutor([]         , [3 4 5])       %       0
stupidTutor([]         , [])            %       0

Upvotes: 2

Jonas
Jonas

Reputation: 74930

Well, turns out there might be a way after all :). We make use of the fact that the numbers have to be strictly positive to be special numbers at all.

%# in case we need to handle empty inputs: replace empty input with 0 or 1, respectively.
a = sum(a(:)',1);
bIsEmpty = isempty(b);
b = sum(b(:)',1); b = max(b,1);

specialNumber = find(a==1:length(a));

maxAB = max(max(a), max(b));

%# "zeros()"
bigVectorForComparisonA = (1:maxAB)*0;
bigVectorForComparisonB = (1:maxAB)*0;

bigVectorForComparisonA(specialNumber) = 1;
bigVectorForComparisonB(b) = 1;

%# instead of &, we add. Find only the smallest match
specialNumberInB = find(bigVectorForComparisonA + bigVectorForComparisonB == 2,1,'first');

out = sum(specialNumberInB) * ~bIsEmpty; %# sum([]) = 0

For a slightly prettier solution that assumes up to 1 special number in a

specialNumber = min(find(a==(1:length(a)));

out = any(b==specialNumber)*sum(specialNumber);

Upvotes: 2

Related Questions