Implementing matching pursuit algorithm - matlab

I have implemented matching pursuit algorithm but i m unable to get the required result.
Here is my code:
D=[1 6 11 16 21 26 31 36 41 46
2 7 12 17 22 27 32 37 42 47
3 8 13 18 23 28 33 38 43 48
4 9 14 19 24 29 34 39 44 49
5 10 15 20 25 30 35 40 45 50];
b=[6;7;8;9;10];
n=size(D);
A1=zeros(n);
R=b;
H=10;
if(H <= 0)
error('The number of iterations needs to be greater then 0')
end;
for k=1:1:H
[c,d] = max(abs(D'*R)); %//'
A1(:,d)=D(:,d);
D(:,d)=0;
y = A1\b;
R = b-A1*y;
end
Output
y=
0.8889
0
0
0
0
0
0
0
0
0.1111
I should get only non-zero value at (2,1) and other values should be zero but I'm getting 2 non-zero value. Can you please help me find out where the error is?
Thanks.

I checked with:
http://www.scholarpedia.org/article/Matching_pursuit
Your functions need to be normalized!
D = D./repmat(sum(D,1),5,1);
I get the following algorithm:
D=[1 6 11 16 21 26 31 36 41 46
2 7 12 17 22 27 32 37 42 47
3 8 13 18 23 28 33 38 43 48
4 9 14 19 24 29 34 39 44 49
5 10 15 20 25 30 35 40 45 50];
D = D./repmat(sum(D,1),5,1);
b=[6;7;8;9;10];
n=size(D);
A1=zeros(n);
R=b;
H=100;
if(H <= 0)
error('The number of iterations needs to be greater then 0')
end;
a = zeros(1,H);
G = zeros(size(D,1),H);
for k=1:1:H
ip = D'*R;
[~,d] = max(abs(ip)); %//'
G(:,k) = D(:,d);
a(k) = ip(d);
R = R-a(k)*G(:,k);
end
% recover signal:
Rrec = zeros(size(R));
for i=1:H
Rrec = Rrec + a(i)*G(:,i);
end
figure();
plot(b);
hold on;
plot(Rrec)
It approximates the signal quite well. But not with D(:,2) at first as expected. Maybe it is a starting point...

Here is the updated code. This is based on the algorithm provided at https://en.wikipedia.org/wiki/Matching_pursuit
clc;
clear all;
D=[1 6 11 16 21 26 31 36 41 46
2 7 12 17 22 27 32 37 42 47
3 8 13 18 23 28 33 38 43 48
4 9 14 19 24 29 34 39 44 49
5 10 15 20 25 30 35 40 45 50];
b=[6;7;8;9;10];
H=10;
for index=1:10
G(:,index)=D(:,index)./norm(D(:,index));
end
G1=G;
n=size(G);
R=b;
if(H <= 0)
error('The number of iterations needs to be greater then 0')
end;
if(H >size(D,2))
error('The number of iterations needs to be less than dictionary size')
end;
bIndex=1:size(G,2);
for k=H:-1:1
innerProduct=[];
for index=1:size(G,2)
innerProduct(index)=dot(R,G(:,index));
end
[c,d] = max(abs(innerProduct));
An(H-k+1)=innerProduct(d);
R = R-(An(H-k+1)*G(:,d));
G(:,d)=[];
strong(H-k+1)=bIndex(d);
bIndex(d)=[];
end
G_new=G1(:,strong);
%% reconstruction
bReconstructed=zeros(size(G_new,1),1);
for index=1:size(G_new,2)
bReconstructed(:,index) = (An(index)*G_new(:,index));
end
b_new=sum(bReconstructed,2)

Yes the atoms in the dictionary must be normalized so that the inner products of the current residual with different atoms can be compared fairly.
You may want to check my OMP implementation which also includes incremental Cholesky updates for the least square step in OMP at https://github.com/indigits/sparse-plex/blob/master/library/%2Bspx/%2Bpursuit/%2Bsingle/omp_chol.m
I have written detailed tutorial notes on OMP in my library documentation at https://sparse-plex.readthedocs.io/en/latest/book/pursuit/omp/index.html
My library sparse-plex contains a C implementation of OMP which is close to 4 times faster than fastest MATLAB implementations. See the discussion here https://sparse-plex.readthedocs.io/en/latest/book/pursuit/omp/fast_omp.html

Related

How can I perform this non circular shift in Matlab? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I am trying to perform a non circular shift in MATLAB. I have
A = [1 2 3 4 5; 0 0 13 14 15; 21 22 23 24 25; 0 0 33 34 35 ; 41 42 43 44 45]
and
B = [1 2 3 4 5; 11 12 13 14 15; 21 22 23 24 25; 31 32 33 34 35 ; 41 42 43 44 45]
How can I shift the indexes of the even rows and is it possible to fill in the empty indexes with an integer of choice?
Desired outputs:
A =
1 2 3 4 5
13 14 15
21 22 23 24 25
33 34 35
41 42 43 44 45
B =
1 2 3 4 5
11 12 13 14 15
21 22 23 24 25
31 32 33 34 35
41 42 43 44 45
Edit: Realized I messed up the desired output of matrix B. I had accidentally omitted some of the elements. Trying to shift the variables over to the right non circularly
Using Indexing and Concatenation
Input Arrays:
A = [1 2 3 4 5;
0 0 13 14 15;
21 22 23 24 25;
0 0 33 34 35 ;
41 42 43 44 45];
B = [1 2 3 4 5;
11 12 13 14 15;
21 22 23 24 25;
31 32 33 34 35;
41 42 43 44 45];
Shifting Even Rows Left:
To shift the arrays you can index the arrays relative to the Shift_Factor in this case 2. You can alternatively use circshift() and overwrite the portions of the array with zeroes/the desired Fill_Value. Here a for loop with an increment value of 2 is used to iterate over all the even rows. Upon each iteration, a partition of the array A(Row,Shift_Factor+1:end) is concatenated to an arbitrary fill array Fill_Value*ones(1,Shift_Factor).
Shift_Factor = 2;
Fill_Value = 0;
[Number_Of_Rows,Number_Of_Columns] = size(A);
for Row = 2: +2: Number_Of_Rows
A(Row,:) = [A(Row,Shift_Factor+1:end) Fill_Value*ones(1,Shift_Factor)];
end
A
Shifting Even Rows Right
Shift_Factor = 2;
Fill_Value = 0;
[Number_Of_Rows,Number_Of_Columns] = size(B);
B = [B zeros(Number_Of_Rows,Shift_Factor)];
for Row = 2: +2: Number_Of_Rows
B(Row,:) = [Fill_Value*ones(1,Shift_Factor) B(Row,1:end-Shift_Factor)];
end
B
Ran using MATLAB R2019b

How to rearrange each image patch into number of column vector [duplicate]

This question already has answers here:
Efficient Implementation of `im2col` and `col2im`
(2 answers)
Closed 7 years ago.
I am working on a project where i have used a image whose size is (512x512)then i have divided the whole image by 8 so that there will be 64x64 block then i have to rearrange each 8x8 image patch into a single column so that new size would be
64x4069. unable to understand how to do it.please help.
Here is my code
enter code here
a=imread('lena.png');
b=double(a);
[r,c]=size(b);
bl=8;
br=r/bl;
bc=r/bl;
It will arrange in such a order that first column would be image patch of (1:8,1:8)next column would be(9:16,9:16)likewise.
If reshape is allowed, permute is definitely allowed.
Assuming both the original and block sub-matrix are square matrices
Here is one approach
out = permute(reshape(A,blSz,size(A,1)/blSz,blSz,[]),[1 3 2 4]);
out = reshape(out,size(out,1)*size(out,1),[]);
Sample inputs:
A = randi(50,8); %// Change it with your original `512x512` matrix
blSz = 2; %// Change it to 8 for your problem
Results:
>> A
A =
31 17 18 10 33 31 43 16
20 40 31 15 34 23 42 6
46 24 10 5 32 23 13 47
1 2 37 29 48 34 31 33
24 9 13 35 11 39 30 24
22 37 46 28 36 18 28 32
24 24 14 22 12 34 44 28
39 8 39 33 6 21 14 33
>> out
out =
31 46 24 24 18 10 13 14 33 32 11 12 43 13 30 44
20 1 22 39 31 37 46 39 34 48 36 6 42 31 28 14
17 24 9 24 10 5 35 22 31 23 39 34 16 47 24 28
40 2 37 8 15 29 28 33 23 34 18 21 6 33 32 33
Using loops as OP requested
A = randi(50,8);
blSz = 2;
nBl = size(A,1)/2;
out = zeros(size(reshape(A,blSz*blSz,[])));
count = 1;
for ii = 1:nBl
for jj= 1:nBl
block = A((jj-1)*blSz + 1:(jj-1)*blSz + blSz, (ii-1)*blSz + 1:(ii-1)*blSz + blSz);
out(:,count) = block(:);
count = count + 1;
end
end
Gives the same result as above!
Alternative for im2col using vectorized approach
newOut = mat2cell(reshape(out,blSz,[]),blSz,repmat(blSz,size(out,2),1));
newOut = cell2mat(reshape(newOut,nBl,[]));

Ask for MATLAB code to detect steady state of data

I have a vector of electrical power consumption data which consists of transient, steady and power off states. I would like to identify steady-state starting point by the following condition:
The 5 consecutive elements of the data have difference values between each adjacent element <= threshold value (for this case, let say =10 W)
The first element that meets the condition shows the starting point of steady-state.
Example:
data = [0 0 0 40 70 65 59 50 38 30 32 33 30 33 37 19 ...
0 0 0 41 73 58 43 34 25 39 33 38 34 31 35 38 19 0]
abs(diff(data)) = [0 0 40 30 15 7 9 12 8 3 2 1 3 4 18 19 ...
0 0 41 32 15 9 14 6 5 4 3 4 3 19 19 0]
The sequences of abs(diff(data)) that meet the condition are 8 3 2 1 3 and 6 5 4 3 4. Therefore, the output should show the 10th data element (=30) and 27th data element (=33) as starting point of steady-state (There are 2 times of steady-state detected).
How would I write MATLAB code for this scenario?
(PS: data = 0 shows power off state)
Here's one approach using nlfilter (if the function is not available, you can implement a sliding window yourself):
data = [0 0 0 40 70 65 59 50 38 30 32 33 30 33 37 19 0 0 0 41 73 58 43 34 25 39 33 38 34 31 35 38 19 0];
difs = abs(diff(data));
% Use sliding window to find windows of consecutive elements below threshold
steady = nlfilter(difs, [1, 5], #(x)all(x <= 10));
% Find where steady state starts (1) and ends (-1)
start = diff(steady);
% Return indices of starting steady state
ind = find(start == 1);

Is there a way to generate a matrix in which each element is defined as 10+row_index + column_index without for loops?

I'm trying to generate a matrix in which each element is defined as 10 * row_index + column_index. The rows and columns may fluctuate up to a 9x9 matrix. For example:
11 12 13 14 15 16
21 22 23 24 25 26
31 32 33 34 35 36
41 42 43 44 45 46
51 52 53 54 55 56
The algorithm is exceedingly simple with for loops, but I've been warned that I should avoid for loops unless absolutely necessary, when dealing with matrices, because they are slower than vector/matrix operations.
What other ways are there to generate such a matrix in Matlab 2012b?
For your particular matrix, it's quite straightforward:
nRows = 4;
nCols = 5;
out = bsxfun(#plus,10*(1:nRows)',1:nCols)
out =
11 12 13 14 15
21 22 23 24 25
31 32 33 34 35
41 42 43 44 45

Matlab: add vector to matrix

I have a 3XN matrix representing a list of 3D coordinates,something like
33 33 33 33 34 34 34 34 34 35 35
17 18 19 20 16 17 18 19 20 16 17
10 10 10 10 10 10 10 10 10 10 10
I want to shift all coordinates by some vector v=[1 2 3], that is add the 3D vector to each column of the matrix.
I know how to do that with a for loop, but how can I do it without a loop? Surely there's a way...
you mean like this?
D=[33 33 33 33 34 34 34 34 34 35 35;
17 18 19 20 16 17 18 19 20 16 17;
10 10 10 10 10 10 10 10 10 10 10 ];
A=[1 2 3]';
C= bsxfun(#plus, D, A)
C =
34 34 34 34 35 35 35 35 35 36 36
19 20 21 22 18 19 20 21 22 18 19
13 13 13 13 13 13 13 13 13 13 13
Use repmat:
M = randn(3, N); % your 3 x N matrix
v = randn(3, 1); % your vector
r = M + repmat(v, [1 N]); % add v to every column of M