how to write a program on matlab

2 次查看(过去 30 天)
hang dong
hang dong 2019-6-13
I have a program written on Matlab as follows:
if nargin <2
error('PERMS1:minrhs','Two Input Arguments are required.')
end
if fix(n) ~= n || fix(b) ~= b ||n < 0 || b < 0 || ~isa(n,'double') || ~isa(b,'double') || ~isreal(n) || ~isreal(b)
error('PERMS1:nnegint','Input Parameters n and b Must be Non-negative Integers');
end
a=factorial(n:-1:1);
if b>a
error('PERMS1:brange','Input Parameter b Should be between 1 and n!')
end
p=zeros(1,n); %initialize output variable
idx=ones(1,n); %initialize loop counters
id=find(a<b,1); %locate the level at which the given index stands
for i=id-1:n-1 %work down to figure out the counter states (idx) in each inner loop
d=mod(b,a(i+1)); %how far down this loop?
idx(i)=floor(b/a(i+1))+(d~=0); %set counter, account for counter resetting
b=d;
if d==0 %adjust indices of inner loops if current loop is reset
for j=i+1:n-1
idx(j)=n-j+1;
end
break;
end
end
m=1:n; %all possible indices
for i=1:n
p(i)=m(idx(i)); %set permutation index according to current counter index
m=m(m~=p(i)); %current premutation index is no longer available
end
n = 8;
x0 = 13294;
c = 21;
x = zeros(1,256);
xi = x0;
i3=zeros(1,n);
for k = 1:256
i2 = floor(sin((pi/2)*((xi/(gt - 1))+((c+1)/(gt + 1))))*(gt - 1));
I2 = perms1(8,i2+1);
I1 = perms1(8,xi+1);
for i=1:n
i3(i)= I1(I2(i));
end
for i = 1:gt
if i3==perms1(8,i)
x(k)=i;
break;
end
end
xi = x(k);
disp(['k=',int2str(k),'x(k)=', int2str(x(k))]);
end
S = zeros(1,256);
for j = 1:256
S(j)= j ;
end
for i = 1 :256
j = floor(x(i)/gt*2^8);
if j==0
j=256;
end
t = 2^8 - i;
if t==0
t=256
end
a = S(j);
S(j)= S(t);
S(t) = a;
end
for i = 1:256
disp(S(i));
end
xlswrite('D:\test.xlsx',S);
I want to convert i, j = 1: 256 to 0: 255, what should I do?
Please help me!

回答(1 个)

Walter Roberson
Walter Roberson 2019-6-13
for j = 0:255
S(j+1)= j+1;
end
for i = 0 :255
j = floor(x(i+1)/gt*2^8);
if j==-1 %cannot happen
j=255;
end
t = 2^8 - (i+1);
if t==-1 %cannot happen
t=255
end
a = S(j+1);
S(j+1)= S(t+1);
S(t+1) = a;
end
for i = 0:255
disp(S(i+1));
end

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by