0

I have the vector numbers which has 6 elements from user input. I want to remove any duplicate value and replace it by another input (without the use of "unique" or similar).

I tried:

myvec=zeros(1,6);
disp('Choose numbers from 1 to 55')
for i=1:6
    myvec(i)=input('');
    if (find(myvec(i)<1 | myvec(i)>55))
        disp('Enter new value')
        myvec(i)=input('');
    end
     if myvec(i+1)==myvec(i)
         myvec(i+1)==input('');
     end
end

The problem is:

1) The statement below is correct?

if myvec(i+1)==myvec(i)
         myvec(i+1)==input('');
       end

2) When running it gives out of bounds because the vector length is 6 and I am trying to access i+1 .I tried to use the for loop from 2:7 but then it adds in the myvec vector the zero as first element.

Thank you!

George
  • 5,808
  • 15
  • 83
  • 160
  • Why the restriction of not using unique? This seems like using a hammer to drive a screw in the wall. Or is it that you're asking to solve your homework? – jpjacobs Feb 25 '13 at 09:53
  • It's not homework.I just want to figure how can I do it this way. – George Feb 25 '13 at 09:55

1 Answers1

1

How about using while loop?

myvec = NaN(1,6);
ii = 0;
disp('Choose numbers from 1 to 55');
while any( isnan(myvec) )
    tmp = input('');
    if tmp > 1 && tmp < 55 
       % proper input. check for duplicate
       if ( ii == 0 ) || ( ii > 0 && all( myvec(1:ii) ~= tmp ) )
           ii = ii+1;
           myvec(ii) = tmp;
       end
    end
end

A small remark, it is best not to use i and j as variables in Matlab.

Community
  • 1
  • 1
Shai
  • 111,146
  • 38
  • 238
  • 371