MATLAB Answers

How to test a value before adding it to the matrix

1 view (last 30 days)
sarah
sarah on 15 Feb 2021
Commented: Rena Berman on 6 May 2021
Hi
In the code below, an matrix named allma contains some duplicate values
The question is what modifications must be added to make the array's values non-duplicate and the size of the matrix is 16 * 16
clear all;clc;
%create the first quadrant of matrix
r=0.2;x(1,1)=0.8;
for i=2:64
x(i)=r+(1-x(i-1))^2;%The first equation
end
A=x;
for i=1: 64
ZA=A(i);
ssA(i)=round(mod(ZA*10^5,256));%To make the values integer between 0 - 255
end
A1=reshape(ssA,8,[]);
for i=1:8
for j=1:8
allma(i,j)=A1(i,j);%Add the values to the large matrix to represent the first quadrant
end
end
%create the second quadrant of matrix
u1=0.15;u2=0.13;Y(1)=0.2; X(1)=0.6;m1=2.76;m2=3.45;n=15;
for i=1:63
X(i+1)=m1*x(i)*(1-x(i))+u1*Y(i)^2;%The second equation
Y(i+1)=m2*Y(i)*(1-Y(i))+u2*((X(i)^2)+X(i)*Y(i));
end
B=X;
for i=1: 64
ZB=B(i);
ssB(i)=round(mod(ZB*10^5,256));%To make the values integer between 0 - 255
end
B1=reshape(ssB,8,[]);
ii=1;
for i=9:16
jj=1;
for j=1:8
allma(i,j)=B1(ii,jj);%Add the values to the large matrix to represent the second quadrant
jj=jj+1;
end
ii=ii+1;
end
%%create the third quadrant of matrix
r = 28; b = 8/3;
yy(1) = 20; z0(1) = 30; Q=10;
for i=2:64
X3d(i)=Q*(Y(i-1)-X(i-1));%The thied equation
yy(i)=r*X(i-1)-yy(i-1)-X(i-1)*z0(i-1);
z0(i)=X(i-1)*yy(i-1)+b*z0(i-1);%The fourth equation
end
C=X3d;
for i=1: 64
ZC=X3d(i);
ssC(i)=round(mod(ZC*10^5,256));%To make the values integer between 0 - 255
end
C1=reshape(ssC,8,[]);
ii=1;
for i=1:8
jj=1;
for j=9:16
allma(i,j)=C1(ii,jj);%Add the values to the large matrix to represent the third quadrant
jj=jj+1;
end
ii=ii+1;
end
%%create the fourth quadrant of matrix
D=z0;
for i=1: 64
ZD=z0(i);
ssD(i)=round(mod(ZD*10^5,256));%To make the values integer between 0 - 255
end
D1=reshape(ssD,8,[]);
ii=1;
for i=9:16
jj=1;
for j=9:16
allma(i,j)=D1(ii,jj);%Add the values to the large matrix to represent the fourth quadrant
jj=jj+1;
end
ii=ii+1;
end
allma =
128 151 252 249 49 224 191 178 0 39 95 255 36 123 157 237
192 60 40 63 172 217 235 242 128 56 46 121 250 175 96 198
192 145 73 180 21 213 187 176 144 37 217 220 154 126 179 44
114 69 136 100 187 223 237 243 121 132 231 242 118 100 44 156
43 11 190 126 255 204 183 175 211 135 164 64 38 55 52 159
2 16 212 130 200 228 239 244 8 183 44 39 63 22 28 161
86 225 80 83 238 197 180 173 66 77 256 125 22 166 146 196
220 174 15 154 209 232 241 245 83 162 166 108 48 116 194 115
96 7 22 171 155 111 225 237 192 37 210 36 0 0 0 0
216 65 140 217 91 156 150 41 128 157 194 160 0 0 0 0
197 71 113 3 232 161 106 253 165 75 250 64 0 0 0 0
188 35 135 128 198 169 228 78 5 252 165 128 0 0 0 0
224 8 93 230 254 17 248 14 172 69 30 0 0 0 0 0
193 184 99 152 2 234 140 132 159 130 41 0 0 0 0 0
124 23 91 201 29 213 202 215 169 38 138 0 0 0 0 0
70 71 49 112 72 153 195 59 179 87 80 0 0 0 0 0
  3 Comments

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 15 Feb 2021
for j=1:8
for i=1:8
if ismember(A1(i,j),allma(1:(j-8)*8+i-1))
allma(i, j) = TrySomethingElse(allma, i, j)) ;
else
allma(i,j)=A1(i,j);%Add the values to the large matrix to represent the first quadrant
end
end
end
Here, TrySomethingElse(allma, i, j) represents a call to a new function that you would write that would somehow invent a new value instead of A1(i, j) to go into allma at position i, j, with the code being careful not to duplicate anything earlier in the matrix.
Please notice that I reversed the order of the for i, for j, so that now for i is the inner loop. This change causes the matrix to be filled "down" the columns instead of across the row. That change makes it easier to test against the values that have already been stored as now the previously stored values are at consecutive linear indices and so canbe extracted in a single indexing call.

Community Treasure Hunt

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

Start Hunting!

Translated by