MATLAB Answers

how create matrix 8*8

9 views (last 30 days)
sarah
sarah on 8 Feb 2021
Commented: Rena Berman on 6 May 2021
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 Comments
Rena Berman
Rena Berman on 6 May 2021
(Answers Dev) Restored edit

Sign in to comment.

Accepted Answer

Jan
Jan on 8 Feb 2021
Edited: Jan on 8 Feb 2021
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 Comments
Image Analyst
Image Analyst on 7 Mar 2021
I've accepted Jan's answer since sarah forgot to.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by