# How to get a random vector whose elements arrangement is the same as that of my desired vector

2 views (last 30 days)
Sadiq Akbar on 30 Sep 2020
Commented: Sadiq Akbar on 2 Oct 2020
If I have a desired vector u=[3 1 5 105 155 50].
Now I want to genrate 10 random vectors of the same size as above u. Then I want to get one of those random vectors which is nearly equal to u and has the same arrangment of elements as that of my desired vector u.
i.e. if one of those generated vector is say for example [1.001 2.99 4.999 154.999 105.001 49.999]. This is nearly equal to my above desired vector u but the arrangement of elements of this vector is not the same as that of my u vector. So I want to get it in the same arrangement of elements as that of my u vector. How can we do this?

#### 1 Comment

Sadiq Akbar on 1 Oct 2020
Dear madhan ravi, the page shows me that you have edited the answer 11 minutes agao, but I cann't see it. What should I do now to see your answer? Regards

madhan ravi on 30 Sep 2020
Edited: madhan ravi on 30 Sep 2020
Use the second output of sort() to the newly sorted generated vector as index.
[~, ix] = sort(u);
N = sort(newly_generated_vector);
Wanted = N(ix)

Sadiq Akbar on 2 Oct 2020
Thank you very much madhan ravi for your tireless efforts. Yes, you are right that we get its logical value 1, but as I have requested you I am not interested in its logical value. Rather I want to make the arrangement of elements in my random vector the same as that of my desired vector. If you run this program, the 1st check gives me CORRECT RESULT. But the 2nd and the 3rd check give me the following WRONG RESULTS respectively though their logical values are 1 .
Wanted= 5 1 3 50 10 30 % Result of check 2
Wanted= 5 1 3 50 10 30 % Result of check 3
Now if you compare arrangement of elements of Wanted vectors of these two results with u1 and u2 where they are our desired vectors respectively, you well see that its different because
u1=[1 3 5 10 30 50]; % Desired vector 1
u2=[3 1 5 30 10 50]; % Desired vector 2
As you can see elements in Wanted of check 2 are 5 1 3 50 10 30 while that of u1 are
1 3 5 10 30 50.
Likewise elements of Wanted of check3 are 5 1 3 50 10 30 while that of u2 are
3 1 5 30 10 50.
I emphasize that I am not interested in its logical values, rahter I want to make the arrangement of elements in the random vector the same as that of my desired vector.
madhan ravi on 2 Oct 2020
Man I get the correct results for God’s sake!
Do you know why isequal() was used or what the function actually does? You seem to be lacking basic MATLAB fundamentals. It means the arrangements are made according to the wanted vector. I have already answered your original question. Are you playing with me or what just to waste my time??
>> u1=[1 3 5 10 30 50]; % Desired vector 1
u2=[3 1 5 30 10 50]; % Desired vector 2
u3=[5 1 3 50 10 30]; % Desired vector 3
%1st check
[~, ix] = sort(u3);
[~, ix1(ix)] = sort(u1);
Wanted = u1(ix1);
u3
Wanted
%2nd check
[~, ix] = sort(u1);
[~, ix1(ix)] = sort(u2);
Wanted = u2(ix1);
u1
Wanted
% 3rd Check
[~, ix] = sort(u2);
[~, ix1(ix)] = sort(u3);
Wanted = u3(ix1);
u2
Wanted
u3 =
Columns 1 through 3
5 1 3
Columns 4 through 6
50 10 30
Wanted =
Columns 1 through 3
5 1 3
Columns 4 through 6
50 10 30
u1 =
Columns 1 through 3
1 3 5
Columns 4 through 6
10 30 50
Wanted =
Columns 1 through 3
1 3 5
Columns 4 through 6
10 30 50
u2 =
Columns 1 through 3
3 1 5
Columns 4 through 6
30 10 50
Wanted =
Columns 1 through 3
3 1 5
Columns 4 through 6
30 10 50
If you can’t analyse the above that it gives the correct results. I don’t know what will make you realise.
Sadiq Akbar on 2 Oct 2020
I am extremely SORRY madhan ravi. No, I don't play with you. Even I don't want to waste anybody's time. Neither I can think like that. Its one's preciuos time that one spares for me. So why would i want to do like that when you are sparing your precious time for me. Actually its my fauklt as I am not as expert as you are. If I were expert, then why would I asked for help. But due to my weakness, I was doing a serious mistake which is that the program which I was running was as follows:
% Help by sir madhan ravi from Mathworks
u1=[1 3 5 10 30 50]; % Desired vector 1
u2=[3 1 5 30 10 50]; % Desired vector 2
u3=[5 1 3 50 10 30]; % Desired vector 3
%%%%%%%%%%%%%%%%%%%
%1st Try of sir madhan ravi
%%%%%%%%%%%%%%%%%%%
% % % [~, ix] = sort(u2);
% % % N = sort(u3);
% % % Wanted = N(ix)
%%%%%%%%%%%%%%%%%%%%%%%%%
% 2nd Try of sir madhan ravi
%%%%%%%%%%%%%%%%%%%%%%%%
% [~, ix] = sort(u3);
% [U1, ix1(ix)] = sort(u1); % If u1 is my newly generated vector for example
% Wanted = U1(ix1) %-----------Correct
% % My Change in u
[~, ix] = sort(u1);
[U1, ix1(ix)] = sort(u2); % If u1 is my newly generated vector for example
Wanted = U1(ix1) % --------------Wrong
% [~, ix] = sort(u2);
% [U1, ix1(ix)] = sort(u3); % If u1 is my newly generated vector for example
% Wanted = U1(ix1) %---------------Wrong
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 3rd Try of sir madhan ravi
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
u1=[1 3 5 10 30 50]; % Desired vector 1
u2=[3 1 5 30 10 50]; % Desired vector 2
u3=[5 1 3 50 10 30]; % Desired vector 3
%%%%%%%%%%%%%%%%
% %1st check
%%%%%%%%%%%%%%%%%%
% [~, ix] = sort(u3);
% [~, ix1(ix)] = sort(u1);
% Wanted = u1(ix1);
% Check_1 = isequal(u3, Wanted)
%%%%%%%%%%%%%%%%%%%%%%%
%2nd check-----------------Wrong Result
%%%%%%%%%%%%%%%%%%%%%%%
[~, ix] = sort(u1);
[~, ix1(ix)] = sort(u2);
Wanted = u2(ix1);
Check_2 = isequal(u1, Wanted)
%%%%%%%%%%%%%%%%%%%%%%
% % 3rd Check
%%%%%%%%%%%%%%%%%%%%%%
% [~, ix] = sort(u2);
% [~, ix1(ix)] = sort(u3);
% Wanted = u3(ix1);
% Check_3 = isequal(u2, Wanted)
I didn't think that there was a semicolon at there end of the Wanted vector in your correct program.At the same time there was no semicolon at the end of the Wanted vector of the wrong program which was also running and was not commented by me. So its my fault and i am extremely sorry for that.
What should I do that your kind heart forgives me?

the cyclist on 30 Sep 2020
Edited: the cyclist on 30 Sep 2020
Do you want something like this, which adds some random "noise" around the elements of u?
u = [3 1 5 105 155 50];
m = 10;
n = size(u,2);
r = zeros(m,n);
for ii = 1:m
r(ii,:) = u + 0.001*(rand(1,n)-0.5);
end

Sadiq Akbar on 30 Sep 2020
Thank you very much madhan ravi for your prompt response. I tested your above program for the following cases:
u1=[1 3 5 10 30 50]; % Desired vector 1
u2=[3 1 5 30 10 50]; % Desired vector 2
u3=[5 1 3 50 10 30]; % Desired vector 3
>>[~, ix] = sort(u1);
N = sort(u2); % If u2 is my newly generated vector for example
Wanted = N(ix)
Wanted =
1 3 5 10 30 50 ----------------------------(1)
>>[~, ix] = sort(u2);
N = sort(u1); % If u1 is my newly generated vector for example
Wanted = N(ix)
Wanted =
3 1 5 30 10 50 ----------------------------(2)
>>[~, ix] = sort(u3);
N = sort(u1); % If u1 is my newly generated vector for example
Wanted = N(ix)
Wanted =
3 5 1 30 50 10 --------------------------(3)
As you can see it gives correct result in case 1 and 2 above because the wanted vector is same as u, but in case of (3), wanted is not same as u.I want to get wanted vector whose element arrangement is same as that of my u in all cases.
Sadiq Akbar on 30 Sep 2020
Thank you very much the cyclist for your prompt response also. I checked your program also. But I don't want like this. I want as I said to madhan ravi above.