Reputation: 41
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
Upvotes: 1
Views: 147
Reputation: 41
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
Upvotes: 0
Reputation: 3194
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.
Upvotes: 0
Reputation: 14939
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
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
Upvotes: 1
Reputation: 36710
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.
Upvotes: 0