matlab:将一维数组的顺序打乱,然后再恢复顺序(自编策略,供参考)

将顺序的索引数组打乱

function index=reorder_index(ind_in, N)
% This function is used to reorder the index of element for a given index-vector,
% then return the reorder index
% N is a number, it represents the number of row and column for matrix
if mod(N,2)==0
	M = N*N;
else
    M = N*N -1
end
% 相邻位置交换,如[1,2,3,4,5,6,7,8,9,10]-->[2,1,4,3,6,5,8,7,10,9]
ind1 = ind_in;
for i=1:M
    if mod(i,2)==0
        ind1(i)=ind_in(i-1);
    else
        ind1(i)=ind_in(i+1);
    end
end

% 前半段的偶数位置与后半段奇数位置交换,[2,1,4,3,6,   5,8,7,10,9]-->[2,10,4,8,6    5,3,7,1,9]
ind2 = ind1;
for i=1:M/2
    if mod(i,2)==1
        ind2(i)=ind1(M-i+1);
        ind2(M-i+1)=ind1(i);
    end
end

% 将数组分为四段,第一三段镜像倒换
ind3 = ind2;
for i=1:M/4
    i;
    j=3*M/4-i+1;
    ind3(i)=ind2(3*M/4-i+1);
    ind3(3*M/4-i+1)=ind2(i);
end

% 转换为矩阵,然后转置,再变为向量
ind_mat = reshape(ind3, [N,N]);
trans_ind_mat = ind_mat';
ind4 = reshape(trans_ind_mat, [1,N*N]);

% 将数组分为两段,前后偶数位置互换
ind5 = ind4;
for i=1:M/2
    if mod(i,2)==0
        i;
        ind5(i)=ind4(i+M/2);
        ind5(i+M/2)=ind4(i);
    end
end

index = ind5;
% 将数组分为四段,第二四段镜像倒换
for i=1:M/4
%     i
%     j = i+ M/4
%     k = M-i+1
    index(i+ M/4)=ind5(M-i+1);
    index(M-i+1)=ind5(i+M/4);
end

sort_index = sort(index);
disp('xxxxxx')
end

恢复索引数组

function index=recover_index(ind_in,N)
if mod(N,2)==0
	M = N*N;
else
    M = N*N -1
end
ind1 = ind_in;
% 将数组分为四段,第二四段镜像倒换
for i=1:M/4
%     i
%     j = i+ M/4
%     k = M-i+1
    ind1(i+ M/4)=ind_in(M-i+1);
    ind1(M-i+1)=ind_in(i+ M/4);
end

% 将数组分为两段,前后偶数位置互换
ind2 = ind1;
for i=1:M/2
    if mod(i,2)==0
        ind2(i)=ind1(i+M/2);
        ind2(i+M/2)=ind1(i);
    end
end

% 转换为矩阵,然后转置,再变为向量
ind_mat = reshape(ind2,[N,N]);
trans_ind_mat = ind_mat';
ind3 = reshape(trans_ind_mat,[1, N*N]);

% 将数组分为四段,第一三段镜像倒换
ind4 = ind3;
for i=1:M/4
    ind4(i)=ind3(3*M/4-i+1);
    ind4(3*M/4-i+1)=ind3(i);
end

% 前半段的偶数位置与后半段奇数位置交换,[2,1,4,3,6,   5,8,7,10,9]-->[2,10,4,8,6    5,3,7,1,9]
ind5 = ind4;
for i=1:M/2
    if mod(i,2)==1
        ind5(i)=ind4(M-i+1);
        ind5(M-i+1)=ind4(i);
    end
end

% 相邻位置交换,如[1,2,3,4,5,6,7,8,9,10]-->[2,1,4,3,6,5,8,7,10,9]
index = ind5;
for i=1:M
    if mod(i,2)==0
        index(i)=ind5(i-1);
    else
        index(i)=ind5(i+1);
    end
end
sort_index = sort(index);
disp('xxxxxx')
end

下面看如何使用上面的函数,实现二维区域函数的画图。

clear all
close all
clc

pi=3.1415926;

q=5;
N=2^q;
xstart = -1;
xend =1;
ystart =-1;
yend = 1;
hx = (xend - xstart)/(N+1);  % the step-size for x-direction
hy = (yend - ystart)/(N+1);  % the step-size for y-direction
[meshx, meshy] = meshgrid(xstart:hx:xend, ystart:hy:yend);

x = meshx(:)';  % changing the mesh-mat of x into a vector, up-->down, then left-->right
y = meshy(:)';  % changing the mesh-mat of y into a vector, up-->down, then left-->right

points = [x;y];

umesh = sin(pi*meshx).*sin(pi*meshy);
figure('name','umesh')
h1 = surf(meshx, meshy, umesh);
hold on
colorbar;
caxis([0 1.0])
ftsz = 14;
set(gca, 'XMinortick', 'off', 'YMinorTick', 'off', 'Fontsize', ftsz);
set(gcf, 'Renderer', 'zbuffer');

N1=N+2;
index2all = linspace(1, N1*N1,N1*N1);
shuffle_index = reorder_index(index2all,N1);
reorder_points = points(:,shuffle_index);
urand = sin(pi*reorder_points(1,:)).*sin(pi*reorder_points(2,:));
figure('name','urand')
urand_mat = reshape(urand,[N1,N1]);
h2 = surf(meshx, meshy, urand_mat);
hold on
colorbar;
caxis([0 1.0])
ftsz = 14;
set(gca, 'XMinortick', 'off', 'YMinorTick', 'off', 'Fontsize', ftsz);
set(gcf, 'Renderer', 'zbuffer');

u_recover = recover_index(urand, N1);
figure('name','u_recover')
ur_mat = reshape(u_recover,[N1,N1]);
h3 = surf(meshx, meshy, ur_mat);
hold on
colorbar;
caxis([0 1.0])
ftsz = 14;
set(gca, 'XMinortick', 'off', 'YMinorTick', 'off', 'Fontsize', ftsz);
set(gcf, 'Renderer', 'zbuffer');

左图为原始顺序图,中间为散乱点图,右侧为重构顺序图
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
下面再看另一种策略,先随机生成一组杂乱的索引,然后对原始一维数据进行打乱和恢复

clc;
clear all
close all
% 示例:还原一维order
original_v = [1, 10, 9, 8,2,3,5,7, 50, 41, 20, 18, 66];
index = randperm(length(original_v));
dis_order = original_v(index);
recover(index) = dis_order;           %recover就是对dis_order进行还原

disp('recover:');
recover
  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值