I need to make an array of zeros and ones in this particular fractal pattern:
0 0 0 0 0 1 0 0 0 0 0 0
0 0 1 1 1 1 1 1 1 0 0 0
0 1 0 0 0 1 0 0 0 1 0 0
0 1 0 0 1 1 1 0 0 1 0 0
0 1 0 1 0 1 0 1 0 1 0 0
1 1 1 1 1 1 1 1 1 1 1 1
0 1 0 1 0 1 0 1 0 1 0 0
0 1 0 0 1 1 1 0 0 1 0 0
0 1 0 0 0 1 0 0 0 1 0 0
0 0 1 1 1 1 1 1 1 0 0 0
0 0 0 0 0 1 0 0 0 0 0 0
0 0 0 0 0 1 0 0 0 0 0 0
The actual array should be 100 x 100. The pattern should start from the middle (x,y) coordinate, and expand to look like the pattern (using loops).
So far, I have only managed to make a pattern that looks like a '+' sign. I am not sure how to continue it.
This is my code so far:
n = zeros(16); % using 16x16 array for practice
x = length(n)/2;
y = length(n)/2;
z = length(n) - 1;
xso = length(n)/2; % x axis south movement
xno = length(n)/2; % x axis north movement
yea = length(n)/2; % y axis east movement
ywe = length(n)/2; % y axis west movement
for i = 1:1:z
newyea = move('east', x, yea);
n(x, newyea) = 1;
yea = newyea;
newywe = move('west', x, ywe);
n(x, newywe) = 1;
ywe = newywe;
newxso = move('south', xso, y);
n(newxso, y) = 1;
xso = newxso;
newxno = move('north', xno, y);
n(newxno, y) = 1;
xno = newxno;
end
I also have a user defined function:
function newval = move(dir, x, y)
switch dir
case 'east'
newval = y + 1;
case 'west'
newval = y - 1;
case 'south'
newval = x + 1;
case 'north'
newval = x - 1;
end
Since there were no restrictions given on the appearance of the loop, I would propose the below solution. But, before, let's have a look at your given example:
Either you should restrict the desired dimension d to be odd, i.e. d = 11, 13, 15, ... or you should specify, how the pattern should be continued in case of an even dimension d, like here d = 12. For my solution, I decided to rely on the dimension d to be odd.
Here's the code:
d = 15; % Dimension
A = zeros(d); % Initialize output array A
c = (d + 1) / 2; % Calculate center index (row, column)
A(:, c) = 1; % Add: Cross
A(c, :) = 1;
J = 0; % Auxiliary index
for I = (c+2):2:d % For every second row (or column) from center to border
J = J + 1;
l = 4 * J - 1; % Calculate length of line to draw
s = c - (l-1)/2; % Calculate start point of line
e = c + (l-1)/2; % Calculate end point of line
A(I, s:e) = 1; % Add: "South" line
A(s:e, I) = 1; % Add: "East" line
A(c - 2*J, s:e) = 1; % Add: "North" line
A(s:e, c - 2*J) = 1; % Add: "West" line
end
figure(1); % Show image
imagesc(A);
Output for d = 15 (to compare to given example):
Output for d = 99:
Hope that helps!
If you have some more stricter limitations on the for loop, let me/us know. Then, I will try to modify my code accordingly.
Related
For example I have a logial vector in MATLAB:
idx1 = [0 0 0 1 1 1 0 0 1 1 1 0 0 0 0 1 1 1 1 1 1 0 0 0 0 0 0 0 1 1 1 0 0]
And I want to get numbers (count each block) of such blocks: 1 1 1, i.e. such block contain N elements == 1 (N "1"). idx1 - array, and his dimension can be any, for example 3820000.
How count many blocks (sequences of ones) occur in the entire array idx1?
counts_idx = 0;
init_counts_idx = 0;
arr = 0;
for i = 1:length(idx1) -1
for kk = 1 : length(idx1) - 1
if idx1(kk + 1) == 1
init_counts_idx = init_counts_idx + 1;
arr = init_counts_idx;
else
init_counts_idx = counts_idx;
end
C = {i,arr};
end
end
I try to using cells...
You can calculate the start and end indices of each block by diff([0 idx1 0]). Then, use this information to calculate block lengths Ns. Finally express the result as a cell array using the function C = mat2cell(A,rowDist).
idx1 = [0 0 0 1 1 1 0 0 1 1 1 0 0 0 0 1 1 1 1 1 1 0 0 0 0 0 0 0 1 1 1 0 0];
diffs = diff([0 idx1 0]);
% find start index of the blocks
loc = find(diffs == 1);
% calc block lengths by subtracting end - start indices
Ns = find(diffs == -1) - loc;
C = mat2cell([loc' Ns'],ones(size(loc)))
4×1 cell array
{[ 4 3]}
{[ 9 3]}
{[16 6]}
{[29 3]}
If you are interested only in the number of such blocks, length(loc) will give you the answer, it is similar to bwconncomp(idx1).NumObjects.
bwconncomp(idx1).NumObjects
See bwconncomp()
I want to plot a cube of side length 10, that would be symmetrical from -5 to 5 and not -6 to 4.
xc=1; yc=1; zc=1; % coordinated of the center
L=10; % cube size (length of an edge)
alpha=0.8; % transparency (max=1=opaque)
X = [0 0 0 0 0 1; 1 0 1 1 1 1; 1 0 1 1 1 1; 0 0 0 0 0 1];
Y = [0 0 0 0 1 0; 0 1 0 0 1 1; 0 1 1 1 1 1; 0 0 1 1 1 0];
Z = [0 0 1 0 0 0; 0 0 1 0 0 0; 1 1 1 0 1 1; 1 1 1 0 1 1];
C='blue'; % unicolor
X = L*(X-0.5) + xc;
Y = L*(Y-0.5) + yc;
Z = L*(Z-0.5) + zc;
fill3(X,Y,Z,C,'FaceAlpha',alpha); % draw cube
axis equal
I just set the center to (0,0,0) to get the desired result:
xc=0; yc=0; zc=0; % coordinates of the center
How can one find the number of separated linked blocks using a symmetric matrix of 0s and 1s in Matlab?
For example in matrix A, if A(n,m)=1, member n and m are connected. Connected elements make blocks. In below matrix, members 2,3,4,5,6,8,9 are connected and make a block. Also, there are two clusters of size equal to 2 and one block of size 7.
A = [1 0 0 0 0 0 0 0 0 0
0 1 1 1 1 1 0 1 1 0
0 1 1 1 1 1 0 1 1 0
0 1 1 1 1 1 0 1 1 0
0 1 1 1 1 1 0 1 1 0
0 1 1 1 1 1 0 1 1 0
0 0 0 0 0 0 1 0 0 0
0 1 1 1 1 1 0 1 1 0
0 1 1 1 1 1 0 1 1 0
0 0 0 0 0 0 0 0 0 1]
following your previous question (link) you can use labeling instead of binary indications and then sqrt the number of members in each block:
A = false(10);
% direct connections
A(2,3) = 1;
A(3,4) = 1;
A(5,6) = 1;
A(4,9) = 1;
A = A | A';
B = double(A | diag(ones(1,10))); % each node is connected to it self
B(B == 1) = 1:nnz(B); % set initial unique labels
while true
B_old = B;
% connect indirect connected nodes
for node = 1:size(B,1)
row = B(node,:);
col = row';
row = row > 0;
col(col > 0) = max(col);
cols = repmat(col,[1 nnz(row)]);
% set the same label for each group of connected nodes
B(:,row) = max(B(:,row) , cols);
end
if isequal(B,B_old)
break
end
end
% get block size
u = unique(B);
counts = hist(B(:),u);
% remove non connections
counts(u == 0) = [];
u(u == 0) = [];
% remove self connections
u(counts == 1) = [];
counts(counts == 1) = [];
% block size
blocksize = sqrt(counts);
I know the code below :
N = 5;
assert(N>1 && mod(N,2)==1);
A = zeros(N);
% diamond mask
N2 = fix(N/2);
[I,J] = meshgrid(-N2:N2);
mask = (abs(I) + abs(J)) == N2;
% fill with zeros
A(mask) = 1;
which transforms matrix A to this:
A=
0 0 1 0 0
0 1 0 1 0
1 0 0 0 1
0 1 0 1 0
0 0 1 0 0
But I want the diamond to be filled with 1.
What should I do?
Here's a vectorized approach using bsxfun -
Nh = (N+1)/2;
range_vec = [1:Nh Nh-1:-1:1];
out = bsxfun(#plus,range_vec(:),range_vec) > Nh
Sample runs -
1) N = 5 :
out =
0 0 1 0 0
0 1 1 1 0
1 1 1 1 1
0 1 1 1 0
0 0 1 0 0
2) N = 9 :
out =
0 0 0 0 1 0 0 0 0
0 0 0 1 1 1 0 0 0
0 0 1 1 1 1 1 0 0
0 1 1 1 1 1 1 1 0
1 1 1 1 1 1 1 1 1
0 1 1 1 1 1 1 1 0
0 0 1 1 1 1 1 0 0
0 0 0 1 1 1 0 0 0
0 0 0 0 1 0 0 0 0
You can use tril and flip functions:
mat = tril(ones(N), round((N-1)/2)) - tril(ones(N), round((-N-1)/2));
out = mat & flip(mat)
Odd values of N:
% N = 5;
out =
0 0 1 0 0
0 1 1 1 0
1 1 1 1 1
0 1 1 1 0
0 0 1 0 0
Even values of N:
% N = 4;
out =
0 1 1 0
1 1 1 1
1 1 1 1
0 1 1 0
What you need is to return a 1 or a 0 based on the Manhattan distance from each array location to the center of your diamond
N = 5;
assert(N>1 && mod(N,2)==1);
A = false(N);
[m, n] = size(A); %dimensions of A
X = floor([m, n]/2); %floored division gives integer indices of center of array
x = X(1); y = X(2);
radius = m/2; %half the height gives the radius
for a = 1 : m
for b = 1 : n
A(a,b) = abs(a-x)+abs(b-y) <= radius; %test if manhatten distance <= radius
end
end
This naturally will need editing to suit your particular case... In particular, the center of your diamond can realistically be placed anywhere by modifying x, y, and the radius can be either smaller or larger than half the width of the array if you so choose.
Just add a for loop and fill all diagonals:
N = 5;
assert(N>1 && mod(N,2)==1);
A = zeros(N);
% diamond mask
N2 = fix(N/2);
[I,J] = meshgrid(-N2:N2);
for id = 0:N2
A((abs(I) + abs(J)) == id) = 1;
end
I am trying to make a parity check matrix from non-systematic to systematic. Hence, I am attaching my code below. Somewhat it is correct, but there are some problems. It would be really great if someone could help me in this.
Subject: Information theory and coding. I am working on LDPC coding and decoding. Please check the code below
MATLAB CODE:
H = [1 0 1 1 0; 0 0 1 0 1; 1 0 0 1 0; 1 0 1 1 1]
[m,n] = size(H);
k = n-m;
for i = k+1:n
%H(:,i)
ind = find(H(:,i),1,'last');
% exchanging (ind)th row and (i-k)th row
if ind < i-k
continue;
end
if ind ~= i-k
temp = H(ind,:);
H(ind,:) = H(i-k,:);
H(i-k,:) = temp;
end
I = find(H(:,i));
% Guassian elimination
for j = 1:length(I)
if I(j) ~= i-k
H(I(j),:) = mod(H(I(j),:)+H(i-k,:),2);
end
end
end
Hsys = H
For e.g.
This is my H matrix:
H =
1 0 1 1 0
0 0 1 0 1
1 0 0 1 0
1 0 1 1 1
I want to have an identity matrix inside the matrix. The dimension on H matrix here is (mxn) which is (4x5).
Generally we use Gaussian elimination method to make the Identity matrix.hence, we make operations between rows. This is how we make it systematic.
I should have matrix as this in the result:
Hsys =
0 1 0 0 0
0 0 1 0 0
1 0 0 1 0
0 0 0 0 1
I should have an identity matrix of dimension m.
Here is how I'd do it (using Gauss-Jordan elimination):
% Not your matrix since it does not have any ones in the second column.
H=[1 1 0 1 1 0 0 1 0 0;
0 1 1 0 1 1 1 0 0 0;
0 0 0 1 0 0 0 1 1 1;
1 1 0 0 0 1 1 0 1 0;
0 0 1 0 0 1 0 1 0 1];
rows = size(H, 1);
cols = size(H, 2);
r = 1;
for c = cols - rows + 1:cols
if H(r,c) == 0
% Swap needed
for r2 = r + 1:rows
if H(r2,c) ~= 0
tmp = H(r, :);
H(r, :) = H(r2, :);
H(r2, :) = tmp;
end
end
end
% Ups...
if H(r,c) == 0
error('H is singular');
end
% Forward substitute
for r2 = r + 1:rows
if H(r2, c) == 1
H(r2, :) = xor(H(r2, :), H(r, :));
end
end
% Back Substitution
for r2 = 1:r - 1
if H(r2, c) == 1
H(r2, :) = xor(H(r2, :), H(r, :));
end
end
% Next row
r = r + 1;
end