Rotate vector in row

3 ビュー (過去 30 日間)
Nikoleta
Nikoleta 2020 年 1 月 12 日
コメント済み: Adam Danz 2020 年 1 月 15 日
Hi, i have an array
n= [1,19,15,18,21,10,16,2,7,3,17,6,1;
1,14,20,8,4,13,9,5,1,0,0,0,0;
1,11,12,1,0,0,0,0,0,0,0,0,0];
What i need to do is to randomly choose 2 elements of each row (but not the ones and the zeros) and rotate the vector in between them .
For example new_n= [ 1,19,15,17,3,7,2,16,10,21,18,6,1;
1,14,13,4,8,20,9,5,1,0,0,0,0;
1,12,11,1,0,0,0,0,0,0,0,0,0]
If it is possible i would like to be able to do it for any array i might have. (the thing is that 1-etc-1 in each row are non equal paths that start at depot 1 and end at it after they go through the other nodes, so i want the rearrangement to happen between the depots)
  4 件のコメント
Adam Danz
Adam Danz 2020 年 1 月 12 日
I see. The next point that needs clarifying is ,"'randomly choose 2 elements of each row (but not the ones and the zeros) ",
Does that mean that any value can be chosen except 1s and 0s or does it mean that the vector between the two end points must not include any 1s and 0s?
For example, is it OK if these two points are chosen?
[0 0 0 0 9 8 7 6 5 4 3 2 1 1 1 1 2 3 4]
% ^...........^
Nikoleta
Nikoleta 2020 年 1 月 12 日
No this is not ok, it means that the two end points should be limited by the first and the last 1. So no change will happen on these elements [1 _____1 0 0 0 0].

サインインしてコメントする。

採用された回答

Thiago Henrique Gomes Lobato
Thiago Henrique Gomes Lobato 2020 年 1 月 12 日
This may solve your problem:
rng(42)
n= [1,19,15,18,21,10,16,2,7,3,17,6,1;
1,14,20,8,4,13,9,5,1,0,0,0,0;
1,11,12,1,0,0,0,0,0,0,0,0,0];
nrev = n;
for idx=1:size(n,1)
% Get the position of the initial 0
Ncolums = find(n(idx,:)==0);
if isempty(Ncolums)
Ncolums = size(n,2);
else
Ncolums = Ncolums(1)-1;
end
Ncolums = Ncolums-2; % Remove first and last 1
% Get a random index permutation between valid index
Index = randperm(Ncolums)+1; % Add offset so it starts by two
Index = sort(Index(1:2));
% Invert row
nrev(idx,Index(1):Index(2)) = n(idx,Index(2):-1:Index(1));
end
nrev
nrev =
1 19 15 18 21 10 16 6 17 3 7 2 1
1 14 20 8 13 4 9 5 1 0 0 0 0
1 12 11 1 0 0 0 0 0 0 0 0 0
  3 件のコメント
Nikoleta
Nikoleta 2020 年 1 月 15 日
Could you help me with something on your solution?
After i end up with the nrev, i have to estimate the cost according to another array ( For example from node 1 to 3 the cost is 11, from 2 to 6 it is 32 etc) and compare it to n's cost. The thing is that i want to run this random change and then the comparison,20 times . (like for random 1:20).In which part of the code should i add it?
Adam Danz
Adam Danz 2020 年 1 月 15 日
The description is unclear. Perhaps a full example would be helpful.

サインインしてコメントする。

その他の回答 (1 件)

Adam Danz
Adam Danz 2020 年 1 月 12 日
編集済み: Adam Danz 2020 年 1 月 12 日
Simpler
n= [1,19,15,18,21,10,16,2,7,3,17,6,1;
1,14,20,8,4,13,9,5,1,0,0,0,0;
1,11,12,1,0,0,0,0,0,0,0,0,0];
% randomly choose 2 elements of each row
% (but not the ones and the zeros)
randIdx = arrayfun(@(i)sort(randsample(find(~ismember(n(i,:),[1,0])),2)),1:size(n,1),'UniformOutput',false);
% rotate the vector in between each index, per row
for i = 1:size(n,1)
n(i, randIdx{i}) = fliplr(n(i, randIdx{i}));
end

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by