You are on page 1of 1

function x = omp(A,b,S) % Orthogonal Matching Pursuit (OMP)

[N,K] = size(A);
if (N ~= size(b))
error('Dimension not matched');
end
x = zeros(K,1);
r = b;
omega = zeros(S,1);
A_omega = [];
cnt = 0;
while (cnt < S)
cnt = cnt+1;
x_tmp = zeros(K,1);
inds = setdiff([1:K],omega); % iterate all columns except for the chosen
ones
for i = inds
x_tmp(i) = A(:,i)' * r / norm(A(:,i)); % sol of min ||a'x-b||
end
[~,ichosen] = max(abs(x_tmp)); % choose the maximum
omega(cnt) = ichosen;
A_omega = [A_omega A(:,ichosen)];
x_ls = A_omega \ b; % Aomega * x_ls = b
r = b - A_omega * x_ls; % update r
end
for i = 1:S
x(omega(i)) = x_ls(i); %x_sparse(i).value;
end

You might also like