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
How to get a random vector whose elements arrangement is the same as that of my desired vector
조회 수: 1 (최근 30일)
이전 댓글 표시
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?
채택된 답변
madhan ravi
2020년 9월 30일
편집: madhan ravi
2020년 9월 30일
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)
댓글 수: 10
madhan ravi
2020년 10월 2일
편집: madhan ravi
2020년 10월 2일
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.
추가 답변 (1개)
the cyclist
2020년 9월 30일
편집: the cyclist
2020년 9월 30일
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
댓글 수: 2
참고 항목
카테고리
Help Center 및 File Exchange에서 Spectral Measurements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!