Set specified indices to zero - matlab

I have two matrices (x1 and x2) with same size. I would like to use the elements equal to zero in x1 to put the sames elements to zero in x2.
The non working solution I've got now follows:
[i j] = find(x1 == 0);
x2(i,j) = 0;
I've also got a working solution which is:
[i j] = find(x1 == 0);
for n=1:length(i)
x2(i(n),j(n)) = 0;
end
thanks!

Try x2(x1 == 0) = 0. For example:
>> x1 = rand(5, 5)
x1 =
0.4229 0.6999 0.5309 0.9686 0.7788
0.0942 0.6385 0.6544 0.5313 0.4235
0.5985 0.0336 0.4076 0.3251 0.0908
0.4709 0.0688 0.8200 0.1056 0.2665
0.6959 0.3196 0.7184 0.6110 0.1537
>> x2 = randi(2, 5, 5) - 1
x2 =
0 1 1 0 1
0 1 0 0 1
1 1 1 1 0
0 1 1 1 1
1 0 0 0 0
>> x1(x2 == 0) = 0
x1 =
0 0.6999 0.5309 0 0.7788
0 0.6385 0 0 0.4235
0.5985 0.0336 0.4076 0.3251 0
0 0.0688 0.8200 0.1056 0.2665
0.6959 0 0 0 0

Related

Generating discrete signal on Matlab

I am trying to generate a constant signal x[n] = 1 for n = 1, 2, 3 and x[n] = 0 otherwise using matlab.
N = -5:1:5;
X = -5:1:5;
i = 1;
for n = N
if (n >= 1 && n <= 3)
X[i] = 1;
else
X[i] = 0;
end
i = i + 1;
end
But it does not work. I am really new using Matlab for discrete signals, so any help would be welcome.
Thank you.
There is no need to use a for iteration in this case. You can accomplish the same using an indexed approach as follows:
N1 = -5:5;
X1 = zeros(1,numel(N1));
X1(N1 >= 1 & N1 <= 3) = 1
N2 = -8:2;
X2 = zeros(1,numel(N2));
X2(N2 >= 1 & N2 <= 3) = 1
N3 = 1:11;
X3 = zeros(1,numel(N3));
X3(N3 >= 1 & N3 <= 3) = 1
This will output:
X1 =
0 0 0 0 0 0 1 1 1 0 0
X2 =
0 0 0 0 0 0 0 0 0 1 1
X3 =
1 1 1 0 0 0 0 0 0 0 0

Hopfield Network Implementation using Perceptron Learning Rule

I am stuck on implementation of the Hopfiled network with perceptron learning rule. The idea here is to learn the weights for a pattern (binary vector) using single-layer perceptron, and then perform associative memory task using standard Hopfield algorithm. However, after I try to recall a stored vector, the output does not converge to the correct pattern.
See the link, Section 13.4, for more detail on Perceptron implementation the code follows.
w = rand(1,21); %weights
d = [0 0 0 0 0 0]; %desired output
eta = .7; %learning rate
x =[1 1 0 0 0 0]; %memorized pattern
z = zeros(6, 21);
z(1,:) = [x(2) x(3) x(4) x(5) x(6) 0 0 0 0 0 0 0 0 0 0 -1 0 0 0 0 0];
z(2,:) = [x(1) 0 0 0 0 x(3) x(4) x(5) x(6) 0 0 0 0 0 0 0 -1 0 0 0 0];
z(3,:) = [0 x(1) 0 0 0 x(2) 0 0 0 x(4) x(5) x(6) 0 0 0 0 0 -1 0 0 0];
z(4,:) = [0 0 x(1) 0 0 0 x(2) 0 0 x(3) 0 0 x(5) x(6) 0 0 0 0 -1 0 0];
z(5,:) = [0 0 0 x(1) 0 0 0 x(2) 0 0 x(3) 0 x(4) 0 x(6) 0 0 0 0 -1 0];
z(6,:) = [0 0 0 0 x(1) 0 0 0 x(2) 0 0 x(3) 0 x(4) x(5) 0 0 0 0 0 -1];
for t = 1:100
index = randperm(6);
for i=1:6
Y(t,index(i)) = z(index(i),:)*w';
y(t,index(i)) = sgn(Y(t,index(i)));
w = w + eta*(d(index(i)) - y(t,index(i)))*z(index(i),:);
end
end
n = 6;
probe = input('Enter the probe vector: ');
signal_vector = 2*probe-1; % Convert probe to bipolar form
flag = 0; % Initialize flag
old = signal_vector; %save original input
while flag ~= 6; % test if old vector is same as new vector
index = randperm(n); %make sequence for asynchronous update
for j = 1:n
v =z(index(j),:)*w';
if v > 0
signal_vector(index(j)) = 1;
x(index(j)) = 1;
elseif v < 0
signal_vector(index(j)) = -1;
x(index(j)) = -1;
end
end
flag = signal_vector*old';
end
disp('The recalled vector is ')
0.5*(signal_vector + 1)
function y = sgn(x)
if x > 0
y = 1;
else
y = -1;
end
end

How to make a parity check matrix from non-systematic to systematic in Matlab? thanks

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

How to address all faces of a cube (3d array) in matlab?

I need to assign boundary values to a 3d box.
Assuming I have z = rand(L,M,N), is there a better way to address all the faces of this box than processing all 6 faces individually, z(:,:,1), z(:,:,end), z(:,1,:), z(:,end,:), z(1,:,:), z(end,:,:)?
This is what I have right now:
L=3;M=4;N=5;
z = rand(L,M,N);
bv = 0;
z([1,end],:,:) = bv;
z(:,[1,end],:) = bv;
z(:,:,[1,end]) = bv;
I would like to be able to do something like z(indices) = bv.
If you have the Image Processing toolbox, using padarray would work:
z = padarray(z(2:end-1,2:end-1,2:end-1), [1 1 1], bv);
This just takes the inner block of the cube and adds 1 copy of bv on all sides.
Not sure if this is any better than your code, but here it goes:
%// Data
L = 3;
M = 4;
N = 5;
z = rand(L,M,N)
newValue = 0;
%// Let's go
indL = false(L, 1, 1);
indM = false(1, M, 1);
indN = false(1, 1, N);
indL([1 end]) = true;
indM([1 end]) = true;
indN([1 end]) = true;
ind = bsxfun(#or, bsxfun(#or, indL, indM), indN); %// linear index of all faces
z(ind) = newValue
Before:
z(:,:,1) =
0.2653 0.7302 0.1078 0.8178
0.8244 0.3439 0.9063 0.2607
0.9827 0.5841 0.8797 0.5944
z(:,:,2) =
0.0225 0.1615 0.0942 0.6959
0.4253 0.1788 0.5985 0.6999
0.3127 0.4229 0.4709 0.6385
z(:,:,3) =
0.0336 0.5309 0.8200 0.5313
0.0688 0.6544 0.7184 0.3251
0.3196 0.4076 0.9686 0.1056
z(:,:,4) =
0.6110 0.0908 0.2810 0.4574
0.7788 0.2665 0.4401 0.8754
0.4235 0.1537 0.5271 0.5181
z(:,:,5) =
0.9436 0.2407 0.6718 0.2548
0.6377 0.6761 0.6951 0.2240
0.9577 0.2891 0.0680 0.6678
After:
z(:,:,1) =
0 0 0 0
0 0 0 0
0 0 0 0
z(:,:,2) =
0 0 0 0
0 0.1788 0.5985 0
0 0 0 0
z(:,:,3) =
0 0 0 0
0 0.6544 0.7184 0
0 0 0 0
z(:,:,4) =
0 0 0 0
0 0.2665 0.4401 0
0 0 0 0
z(:,:,5) =
0 0 0 0
0 0 0 0
0 0 0 0

Indexing of 2D array in matlab

I have a 6X4 matrix M1 containing only zeros.
I also have two 1D arrays Y1 and Y2 each with length 4.The two arrays contain the desired index values. Now, I want to set(convert to 1) the elements of matrix M1 such that
M1(Y1:Y2) is equal to 1
for ex: Y1=[1 2 2 1] and Y2=[3 4 5 3]
then, M1 should be
1 0 0 1
1 1 1 1
1 1 1 1
0 1 1 0
0 0 1 0
0 0 0 0
I can do this using for loop. But is there any optimised way to do it? (I intend to use much bigger matrices)
use cumsum!
>> szM = size(M1);
>> M1( sub2ind( szM, Y1, 1:szM(2) ) ) = 1
M1 =
1 0 0 1
0 1 1 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
>> M1( sub2ind( szM, Y2+1, 1:szM(2) ) ) = -1
M1 =
1 0 0 1
0 1 1 0
0 0 0 0
-1 0 0 -1
0 -1 0 0
0 0 -1 0
>> M = cumsum(M,1)
M =
1 0 0 1
1 1 1 1
1 1 1 1
0 1 1 0
0 0 1 0
0 0 0 0
A pitfall:
If any of Y2 equals 6 than setting Y2+1 to -1 will exceed matrix dimension.
To fix this you can add two lines before setting to -1 the elements of M:
>> cols = 1:szM(2);
>> sel = Y2 < szM(1);
>> M1( sub2ind( szM, Y2(sel)+1, cols(sel) ) ) = -1
A spin-off for Pavan Yalamanchili's answer using bsxfun: (hover to see:)
using bsxfun without offsets:
M1 = bsxfun( #ge, (1:size(M1,1))', Y1 ) & bsxfun( #le, (1:size(M1,1))', Y2 );
There may be other techniques, but this uses element wise operations which are insanely parallel.
A very simple solution. Thanks #Shai
>> [rows, cols] = size(M);
>> Y1=[1 2 2 1]; Y2=[3 4 5 3];
>> M = bsxfun(#ge, (1:rows)', Y1) & bsxfun(#le, (1:rows)', Y2)
M =
1 0 0 1
1 1 1 1
1 1 1 1
0 1 1 0
0 0 1 0
0 0 0 0
Unnecessarily complicated code
[rows, cols] = size(M);
offsets = ((1 : cols) - 1) * rows
Y1 = offsets + Y1;
Y2 = offsets + Y2;
M = reshape(1:numel(M), rows, cols);
M = bsxfun(#ge, M, Y1) & bsxfun(#le, M, Y2);