how to reverse a random permutation of a matrix to get back the original matrix?

9 ビュー (過去 30 日間)
O = key1(randperm(numel(key1)));
here O is the random permutation of rows and columns of a 'key1' matrix which is '16*147'. how to get back my original key1 matrix?
  2 件のコメント
shashwat soni
shashwat soni 2018 年 9 月 17 日
yes, but i got the answer
shuaib ahmed
shuaib ahmed 2019 年 6 月 13 日
How did you do it?

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

採用された回答

Andrei Bobrov
Andrei Bobrov 2018 年 9 月 17 日
key1 = randi(398,4,5);
s = size(key1);
p = reshape(randperm(numel(key1)),s);
O = key1(p);
[~,ii] = sort(p(:));
out = O(reshape(ii,s));
  1 件のコメント
Bruno Luong
Bruno Luong 2018 年 9 月 17 日
編集済み: Bruno Luong 2018 年 9 月 17 日
alternatively one can do indexing on the lhs and avoid sorting
out(p) = O;
out = reshape(out,s);

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

その他の回答 (1 件)

Image Analyst
Image Analyst 2018 年 9 月 17 日
See attached demo.
  2 件のコメント
shashwat soni
shashwat soni 2018 年 9 月 17 日
in my case the image(original) matrix is a 2d matrix, now i want to generate 3 random integers for each element of a row in the matrix, for e.g. if my matrix is 16*49, so i want a 16*147(147 = 49*3) matrix so that each element of a row in the original matrix gives me 3 random elements. And again i should be able to decrypt it
Image Analyst
Image Analyst 2018 年 9 月 17 日
My code would give 16*49 numbers, which would keep each pixel together. In other words, the green and blue values move to the same location as the red value. This has the effect of simply shuffling the pixels to new locations while keeping the color of the pixel the same (because red, green, and blue all move together to the same new location).
I'm not sure if you want 3 values for each row, or if you want 3 values for each pixel - you seemed to say/indicate both. If you wanted 3 values for each row, I'm not sure how that would look. If you wanted 3 values for each pixel then you could be moving the red, green, and blue values to different locations. To do that with my code you'd simply get 3 sort orders, one for each color channel:
% Get the orders to scramble them in.
scrambleOrderR = randperm(rows*columns);
scrambleOrderG = randperm(rows*columns);
scrambleOrderB = randperm(rows*columns);
% Scramble according to the scrambling order for each individual color channel.
redChannel = redChannel(scrambleOrderR);
greenChannel = greenChannel(scrambleOrderG);
blueChannel = blueChannel(scrambleOrderB);

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

カテゴリ

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