Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

To see different design and perspective, how can we redesign this code and make shorter/longer it?

조회 수: 1 (최근 30일)
Hi, I solved this problem but I want to use my code more efficient manner so, how can we make shorter my code part for this question?
I'll write my solution below.

답변 (1개)

Alice Zurock
Alice Zurock 2020년 5월 2일
% SparseLab is used here (downloaded from https://sparselab.stanford.edu/)
% It can be replaced with any other sparse construction tool/algorithm.
%-------------------------------------------------------------------------
% addpath(genpath('C:\research\pocs\SparseLab2.1-Core'));
N=30;
K=5;
l=100;
rep = 500;
theta = zeros(l,1);
theta(1:K) = randn(K,1);
X = randn(N,l)*(1/sqrt(N));
y = X*theta;
% Question (a)
sols = SolveLasso(X, y);
error =norm(sols-theta);
disp(error)
% Question (b)
Err = zeros(rep,1);
for epan=1:rep
X = randn(N,l)*(1/sqrt(N)); %X=X*diag(1./sqrt(sum(X.^2)));
y = X*theta;
sols = SolveLasso(X, y,length(theta),'lasso');
errorX =norm(sols-theta);
% disp(errorA)
Err(epan) = errorX<10^(-8) + 0;
end
probrandn = sum(Err)/rep;
fprintf('Random Sensing Mtx: %2.2f \n',probrandn)
% Question (c)
Err = zeros(rep,1);
for epan=1:rep
% Construct DCT based sensing matrix
X = dctmtx(l); X=X(randperm(l,N),:);
y = X*theta;
sols = SolveLasso(X, y,length(theta),'lasso');
errorX =norm(sols-theta);
% disp(errorA)
Err(epan) = errorX<10^(-8) + 0;
end
probDCT = sum(Err)/rep;
fprintf('DCT Sensing Mtx: %2.2f \n',probDCT)
% Question (d)
p = [1,9,25,36,64];
Err = zeros(rep,1);
for pval = p
for epan=1:rep
% Construct sparse sensing matrix
OK = false;
while ~OK
kk = zeros(N*l,1);
P = randperm(N*l);
numofzeros = round(N*l*(1-1/sqrt(pval)));
P(N*l-numofzeros+1:end) = [];
kk(P) = sqrt( (sqrt(pval)/N) )*ones(length(P),1).*sign(randn(length(P),1));
X = zeros(N,l);
X(1:l*N) = kk;
% Check if it is full rank
OK = rank(X)==N;
end
y = X*theta;
sols = SolveLasso(X, y,length(theta),'lasso');
errorX =norm(sols-theta);
% disp(errorA)
Err(epan) = errorX<10^(-8) + 0;
end
probsparse = sum(Err)/rep;
fprintf('Sparse Sensing Mtx, p=%i: %2.2f \n',pval,probsparse)
end
disp('')

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by