1

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!

16
  • Is this homework? Are a and b vectors, or can they be higher-dimensional arrays (matrices, etc.?). Commented Nov 19, 2014 at 0:01
  • vectors.. managed to make it work with "ismember".. but can't use this function. Commented Nov 19, 2014 at 0:09
  • (a and b are 1 dimentional arrays) Commented Nov 19, 2014 at 0:10
  • 2
    This is a really contrived homework problem. Not being able to use any of the specialized functions in the comments is like telling someone to cook a meal using only rocks and fire. Having those tools available is what makes MATLAB unique. If you can't use them, then there's no point in using MATLAB. Commented Nov 19, 2014 at 14:57
  • 2
    @rayryeng: I agree. It might teach a bit about indexing, though (see my solution), but rather than making people do puzzles, why not let them do a problem where indexing is actually useful in a real-world problem? Commented Nov 19, 2014 at 17:53

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

7 Comments

I love your function name - +1.
@rayryeng I drew inspiration from your comments :)
It originally didn't say anything about matrices which is what my solution had. The OP corrected me and said they weren't allowed. He or she has changed the specifications at least three times since this post was created. That is probably contributing to my disdain of this question.... And the fact that it's completely contrived.
@rayryeng ffs... I give up on this question as well. Let it be downvoted into oblivion.
@Jubobs - Couldn't agree more my friend.
|
2

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);

9 Comments

+1 - This is what I had in mind essentially, but got miffed when I couldn't use &. Thank you for solving this God awful problem.
The question contains the following spec: If b or a are empty, output is 0. Your approach fails if a is empty; you get a Matrix dimensions must agree error.
@Jubobs: I do not think that there's a requirement for handling empty inputs. Anyway, I can fix that.
unique() is not something we are allowed to use. Sorry, this question is a pain in the ass. By the way, if a or b are empty, 0 is displayed.
@Osh24 - No offense, but your MATLAB tutor sucks. They should be teaching you how to use MATLAB efficiently, not getting you to jump around and do something in 10 lines of code when it can be done in 2. This is probably the most frustrating problem I have ever tried to answer and have read in my history of answering questions here on StackOverflow. Half of it is due to you forgetting requirements and you having to constantly modify your problem.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.