1
clc
clear all
q=[2 31 6;2 31 7;2 31 6;2 31 6;2 31 6;2 31 6];
for i=1:6
    if i>1
        for j=1:i-1
            if q(j,2)==q(i,2)
                e = rand(10,1); % some matrix you want to sample from
                idx5=randi(length(e)); % random index into x
                q(j,2)=idx5
                j=1;
            end
        end
    end
end

I have matrix q ,I want to change the value in the second column in each row if the next value which means that the value in the next row is the same as the value in the previous row ex: row 1 the value in the second column is 31 row 2 the value in the second column is 31

so I want to change the second value which is 31 into a value by this random function when the value is changed ,the code suppose to start checking again starting from all previous values because maybe the new random value is matching again one of the previous values

the problem in the code that it is leaving the loop which is j=1:i-1 when i make j=1 again because i must start checking again all the values from the beginning

I checked by tracing that it is leaving the loop and it is start from loop for i=1:6 but it is suppose to stay in the second loop which is for j=1:i-1 till no previous random value is matching

so I want to make j=1 whenever the values is matching in the matrix,which mean starting the loop again from the beginning

4 Answers4

1

If I understand the question right, this should work:

rand_matrix = [zeros(10,1); rand(10, 1)]; % half of it zeros
ids = find(diff(q(:, 2)) == 0) + 1;
index = ceil(rand(1, length(ids)) * length(rand_matrix)); % randi is better, but i dont remember the correct syntax to get a vector with given max value
q(ids, 2) = rand_matrix(index);

With your sample data, I get (I multiplied the rand_matrix by 40, to get numbers in the same range):

q = 
    2.00000   31.00000    6.00000
    2.00000   14.43287    7.00000
    2.00000    5.46286    6.00000
    2.00000   15.44111    6.00000
    2.00000    0.00000    6.00000
    2.00000    0.00000    6.00000

Update:

You could do like this:

it = 0;  % Counter to avoid looping to inf
while length(unique(q(:,2))) ~= length(q(:,2)) && it < 40   
    % Fill in the blanks
    %
    %
end
Stewie Griffin
  • 14,889
  • 11
  • 39
  • 70
  • it seems no necessary for making half of it is to be zero ,but the values must be exact numbers and whenever the values is matching, the loop must start again at for j=1:i-1 – mujtaba allawati Dec 13 '13 at 11:18
  • What do you mean? Which values must be exact numbers? And why must the loop start again? Is the resulting `q` not what you wanted to achieve? If so, what is wrong? Why do need to use loops? – Stewie Griffin Dec 13 '13 at 11:25
  • i want to change the values in matrix q which is in column 2 so the values is not the same as the previous raw if you see my original matrix all the values in column 2 is 31 so my code suppose to change the value 31 which is in the second raw till the end by random variable but even that i dont want the random variable also to be repeated thats why i have to start the loop again – mujtaba allawati Dec 13 '13 at 11:28
  • no zero will be in the random function,no need half to be zero e = rand(10,1); idx5=randi(length(e)); – mujtaba allawati Dec 13 '13 at 11:45
  • the loop must be reset if the condition satisfied – mujtaba allawati Dec 13 '13 at 12:00
  • yeah replacing for loop by while loop – mujtaba allawati Dec 13 '13 at 12:55
  • 1
    clc clear all q=[2 31 6;2 31 7;2 31 6;2 31 6;2 31 6;2 31 6]; t3=[1 2 3 4 5 6 7 8 9 10]; for i=1:6 if i>1 j=0; while j – mujtaba allawati Dec 13 '13 at 17:02
0

I don't really understand your problem description, thus I took a closer look at your code.

The line j=1 is useless. The for-loop overwrites the iterator. Check this simple example to understand:

for idx=1:10
  disp(idx)
  idx=1;
end

Another block of code that does not produce any output is:

randomval = e(idx5);
if randi(2)-1  % half the time assign to zero
   randomval = 0;
end

randomval is never used.

Daniel
  • 36,610
  • 3
  • 36
  • 69
  • yes you are right no need for randmoval,but the question is how to make the loop to start from beginning whenever the values is matching so to be sure no value is repeated again ,which mean in your case as you explained j=1 must be considered not to be useless – mujtaba allawati Dec 13 '13 at 11:21
0

Here a solution with a recursive function. I only tried it with the q example you gave. Let me know if there are cases that don't work.

function [qMatrix] = testRecur(rowIndex, qMatrix)
% For your example I called the function as follow:
% qMatrix = testRecur(1, [2 31 6;2 31 7;2 31 6;2 31 6;2 31 6;2 31 6]) 
   for ii=rowIndex:(size(qMatrix,1) - 1)
       if qMatrix(ii, 2) == qMatrix(ii+1, 2)
           e = rand(10, 1);
           idx5 = randi(length(e));
           qMatrix(ii+1, 2) = idx5;
           break
       end
   end
   if exist('e','var')
       [qMatrix] = testRecur(ii, qMatrix);
   end
end

It is a good pratice to use ii and jj.

Community
  • 1
  • 1
m_power
  • 3,156
  • 5
  • 33
  • 54
0
for i=1:6
if i>1
    j=0;
    while j<i-1
    j=j+1;
        if q(j,2)==q(i,2)
                e = rand(5,1); % some matrix you want to sample from
                idx5=randi(length(e)); % random index into 
                q(i,2)=idx5
                j=1;

        end      
    end
end
end