help-octave
[Top][All Lists]

## Re: Condensing indices in a matrix

 From: Brett Green Subject: Re: Condensing indices in a matrix Date: Sun, 20 Dec 2020 14:22:06 -0500

On Tue, Dec 8, 2020 at 1:19 AM Kai Torben Ohlhus <k.ohlhus@gmail.com> wrote:

Was it possible to send some minimal working example how array M is
created?  For u(x,y,z), maybe `sub2ind` [1] can be helpful.

HTH,
Kai

Thank you and please excuse the slow response. Here is an MWE:

ind = @(T,n,m) 2*N*(m-1) + 2*(n-1) + T + 1;
M = zeros(2*N^2, 2*N^2);

for n=1:N
for m=1:N
M( ind(0,n,m) , ind(1,n,m) ) = 1;
M( ind(1,n,m) , ind(0,n,m) ) = 1;

M( ind(0,n,m) , ind(1,n+1,m) ) = 1;
M( ind(1,n+1,m) , ind(0,n,m) ) = 1;

M( ind(0,n,m) , ind(1,n,m+1) ) = 1;
M( ind(1,n,m+1) , ind(0,n,m) ) = 1;
end
end

While it's nice that sub2ind exists, I prefer to do the indexing myself so that it is explicitly readable from the code, and so that I can control it directly. The key problem for me is the assignment, which I would like to do without for loops if possible.

reply via email to