Re: Condensing indices in a matrix

Brett Green <[email protected]>
Newsgroups gmane.comp.gnu.octave.general
Message-ID <CANkKLCf4_FVHAaS-4ZbNRS6GqJdBAYDXQ9iGKvX+o4N0qrguVQ@mail.gmail.com>
On Tue, Dec 8, 2020 at 1:19 AM Kai Torben Ohlhus <[email protected]> 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
>
> [1] https://octave.org/doc/v6.1.0/Advanced-Indexing.html
>
>
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.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.