I have two matrices B and D, where B is a matrix and D is a matrix defined as follows:
with ,
where is the identity matrix and denotes the Kronecker product of matrices P and Q. I am currently computing B as a sparse matrix as follows:
o = ones(n,1);
z = zeros(n,1);
D = spdiags([-o o z],-1:1,n,n);
D(1,1) = 0;
I = speye(n);
B1 = kron(I, D);
B2 = kron(D, I);
B = [B1;B2];
I know that can be quickly computed using
[0; diff(x)]
I need to find a quick way of computing and in a similar manor. Currently my code is spending a signifigant amount of its time to compute and so I was wondering if anyone had some incite into computing this efficently.

 채택된 답변

Chris J
Chris J 2020년 3월 9일

0 개 추천

I solved it here is my solution:
U = reshape(x, [n,n]);
u1 = [zeros([1,n]); diff(U, 1, 1)];
u2 = [zeros(n, 1), diff(U, 1, 2)];
y = [u1(:); u2(:)];
To solved for we solve the equavalent formulation :
x1vec = x(1:end/2);
x2vec = x(end/2+1:end);
x1mat = reshape(x1vec, [n,n]);
x2mat = reshape(x2vec, [n,n]);
p1 = -diff(x1mat, 1, 1);
p1 = [-x1mat(2, :); p1(2:end,:); x1mat(end,:)];
p1 = p1(:);
p2 = -diff(x2mat, 1, 2);
p2 = [-x2mat(:,2), p2(:, 2:end), x2mat(:,end)];
p2 = p2(:);
y = p1+p2;

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품

릴리스

R2019b

질문:

2020년 3월 9일

답변:

2020년 3월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by