how create matrix 8*8
조회 수: 21 (최근 30일)
이전 댓글 표시
Hi, can someone help me with this code
I want to create a 8*8 matrix using equation
No value should be repeated within the array
That is, each value in the array appears one time only.
clear all;clc;
r=0.2;x(1,1)=0.8;
for i=2:64
x(i)=r+(1-x(i-1))^2;
end
A=reshape(x,8,[]);
for i=1:8
for j=1:8
allma(i,j)=A(i,j);
end
end
ssA= [];b=[]
for i=1: 8
for j=1:8
ZZZZ=A(i,j);
ssA(i,j)=round(mod(ZZZZ*10^5,256));
end
end
ssA =
128 151 252 249 49 224 191 178
192 60 40 63 172 217 235 242
192 145 73 180 21 213 187 176
114 69 136 100 187 223 237 243
43 11 190 126 255 204 183 175
2 16 212 130 200 228 239 244
86 225 80 83 238 197 180 173
220 174 15 154 209 232 241 245
댓글 수: 4
Jan
2021년 3월 7일
편집: Jan
2021년 3월 7일
@sarah: You have removed the code again in this and several other of your questions. If you have a good reasons to delete it, contact one of the editors or the admins of the forum and explain the problem.
For the readers: Here is the code which was part of the question initially:
clear all;clc;
r=0.2;x(1,1)=0.8;
for i=2:64
x(i)=r+(1-x(i-1))^2;
end
A=reshape(x,8,[]);
for i=1:8
for j=1:8
allma(i,j)=A(i,j);
end
end
ssA= [];b=[]
for i=1: 8
for j=1:8
ZZZZ=A(i,j);
ssA(i,j)=round(mod(ZZZZ*10^5,256));
end
end
ssA =
128 151 252 249 49 224 191 178
192 60 40 63 172 217 235 242
192 145 73 180 21 213 187 176
114 69 136 100 187 223 237 243
43 11 190 126 255 204 183 175
2 16 212 130 200 228 239 244
86 225 80 83 238 197 180 173
220 174 15 154 209 232 241 245
채택된 답변
Jan
2021년 2월 8일
편집: Jan
2021년 2월 8일
Your code does not produce unique values. To obtain uniqueness the 8*8 elements must contain the values from 0 to 63:
v = 0:63;
ssA = reshape(v(randperm(numel(v))), 8, 8)
Or with more possible values:
v = 0:255;
ssA = reshape(v(randperm(numel(v), 64)), 8, 8)
This solution is working, but does not use your equation x(i)=r+(1-x(i-1))^2 . If using this is required, collect the needed 64 unique elements at first:
x = zeros(8, 8);
r = 0.2;
xi = 0.8;
x(1) = round(mod(xi*1e5, 256));
k = 2;
limit = 1e7; % Avoid infinite loop
count = 0;
while k < 64 && count < limit
xi = r + (1 - xi)^2;
xr = round(mod(xi * 1e5, 256));
if ~any(x(1:k-1) == xr) % Check if value is new
x(k) = xr; % Store new value
k = k + 1; % Proceed to next element
end
count = count + 1; % Advance security counter
end
if count > limit
error('Cannot find 64 different values in %d iterations.', limit)
end
댓글 수: 2
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!