MATLAB Answers

how create matrix 8*8

7 ビュー (過去 30 日間)
sarah
sarah 2021 年 2 月 8 日
コメント済み: Rena Berman 2021 年 5 月 6 日 20:23
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 件のコメント
Rena Berman
Rena Berman 2021 年 5 月 6 日 20:23
(Answers Dev) Restored edit

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

採用された回答

Jan
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 件のコメント
Image Analyst
Image Analyst 2021 年 3 月 7 日
I've accepted Jan's answer since sarah forgot to.

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

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by