how create matrix 8*8

7 Ansichten (letzte 30 Tage)
sarah
sarah am 8 Feb. 2021
Kommentiert: Rena Berman am 6 Mai 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 Kommentare
Jan
Jan am 7 Mär. 2021
Bearbeitet: Jan am 7 Mär. 2021
@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.
With participating in this forum, you agree with the Guidelines .
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
Rena Berman
Rena Berman am 6 Mai 2021
(Answers Dev) Restored edit

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Jan
Jan am 8 Feb. 2021
Bearbeitet: Jan am 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 Kommentare
sarah
sarah am 8 Feb. 2021
thank you so much
Image Analyst
Image Analyst am 7 Mär. 2021
I've accepted Jan's answer since sarah forgot to.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by