From: "Nasser M. Abbasi" <nma@12000.org>
Subject: Any easy/build-in construct to extract submatrices from larger matrix?
Date: Sun, 24 Jun 2012 03:05:37 -0500
Date: 2012-06-24T03:05:37-05:00 [thread overview]
Message-ID: <js6hof$o9g$1@speranza.aioe.org> (raw)
Given square matrix, say 3 by 3 real matrix, and we
want to extract a submatrix from it, a 2 by 2 submatrix,
which is found by removing column and row given by
index I and J.
For example, given
A : constant Real_Matrix :=
(( 1.0, 2.0, 3.0),
( 4.0, 5.0, 6.0),
( 7.0, 8.0, 9.0));
and I=1, J=1, then the submatrix to extract is
5 6
8 9
because we removed row I=1, and column J=1, and what
is left is what is needed.
Ofcourse I can code it the hard way, using lots of if's,
and loops, and build the submatrix by hand, one row/column at
a time. But I wanted to see if there is a build-in
easy way, like slicing off a row and column off a matrix
on the fly.
Here is a skeleton code if someone wants to give this a try
---------------------------------------------
with Ada.Text_Io; use Ada.Text_Io;
with Ada.Float_Text_Io; use Ada.Float_Text_Io;
with Ada.Numerics.Real_Arrays; use Ada.Numerics.Real_Arrays;
procedure foo is
A : constant Real_Matrix :=
(( 1.0, 2.0, 3.0),
( 4.0, 5.0, 6.0),
( 7.0, 8.0, 9.0));
B : real_matrix(1 .. 2, 1 .. 2);
begin
FOR I in A'range(1) LOOP
FOR J in A'range(2) LOOP
--B := -- ??? smart way to do this?
put(A(I,J));
END LOOP;
new_line;
END LOOP;
end foo;
--------------------------------
fyi, modern Fortran has a nice facility to do this, called
pack(). It works like this: make up a MASK matrix of same size
as A. For those elements we want removed, put in a logical
.false at that location.
Hence, to extract column 1 say from A, build up a MASK matrix
with .false. in each element of the first column, then call
PACK() with this mask and A. Then PACK() return all elements
of A that has corresponding .true. in the MASK.
So, the above in Fortran works like this:
--------------------------------------------------
program t46
implicit none
integer, parameter :: n=3
integer :: i,j
real (kind=kind(0.0d0)) :: A(n,n),B(n-1,n-1)
logical :: mask(n,n)
A(1,:) = [1, 2, 3];
A(2,:) = [4, 5, 6];
A(3,:) = [7, 8, 9];
DO j=1,n
DO i=1,n
mask = .true.
mask(:,j) = .false.
mask(i,:) = .false.
!-- extract submatrix (looking for Ada equivalent)
B = reshape(pack(A, mask),[n-1,n-1])
write(*,'(2F6.1)') B
print *,'------------'
END DO
END DO
end program t46
------------------------------
>gfortran -fcheck=all -Wall t46.f90
>./a.out
5.0 8.0
6.0 9.0
------------
2.0 8.0
3.0 9.0
------------
2.0 5.0
3.0 6.0
------------
4.0 7.0
6.0 9.0
------------
1.0 7.0
3.0 9.0
------------
1.0 4.0
3.0 6.0
------------
4.0 7.0
5.0 8.0
------------
1.0 7.0
2.0 8.0
------------
1.0 4.0
2.0 5.0
------------
>
I have googled on-line looking for something build-in, but
so far, no luck. But will continue searching...
thanks,
--Nasser
next reply other threads:[~2012-06-24 8:05 UTC|newest]
Thread overview: 41+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-06-24 8:05 Nasser M. Abbasi [this message]
2012-06-24 8:24 ` Any easy/build-in construct to extract submatrices from larger matrix? Dmitry A. Kazakov
2012-06-24 8:55 ` Nasser M. Abbasi
2012-06-24 11:10 ` Dmitry A. Kazakov
2012-06-24 12:16 ` Nasser M. Abbasi
2012-06-24 16:11 ` Dmitry A. Kazakov
2012-06-24 17:14 ` Nasser M. Abbasi
2012-06-24 18:33 ` Dmitry A. Kazakov
2012-06-25 5:44 ` J-P. Rosen
2012-06-25 6:32 ` Nasser M. Abbasi
2012-06-25 7:54 ` J-P. Rosen
2012-06-25 8:20 ` Dmitry A. Kazakov
2012-06-25 14:21 ` Nasser M. Abbasi
2012-06-25 8:36 ` Georg Bauhaus
2012-06-25 13:17 ` Nasser M. Abbasi
2012-06-26 11:44 ` Georg Bauhaus
2012-06-25 16:49 ` Pascal Obry
2012-06-25 17:36 ` Nasser M. Abbasi
2012-06-25 18:49 ` stefan-lucks
2012-06-26 4:41 ` Nasser M. Abbasi
2012-06-26 7:15 ` Jacob Sparre Andersen
2012-06-26 9:51 ` Nasser M. Abbasi
2012-06-25 9:33 ` Simon Wright
2012-06-26 2:48 ` Jerry
2012-06-26 4:19 ` Nasser M. Abbasi
2012-06-26 7:06 ` Nasser M. Abbasi
2012-06-26 12:54 ` Robert A Duff
2012-06-26 15:19 ` Adam Beneschan
2012-06-26 21:14 ` Robert A Duff
2012-06-27 23:42 ` Shark8
2012-06-26 7:10 ` Dmitry A. Kazakov
2012-06-26 15:06 ` Adam Beneschan
2012-06-26 21:19 ` Robert A Duff
2012-06-26 21:40 ` Adam Beneschan
2012-07-03 4:22 ` Randy Brukardt
2012-07-03 8:37 ` Dmitry A. Kazakov
2012-07-05 1:33 ` Randy Brukardt
2012-07-05 7:08 ` Dmitry A. Kazakov
2012-07-06 23:47 ` Randy Brukardt
2012-07-07 8:22 ` Dmitry A. Kazakov
2012-07-05 18:56 ` Adam Beneschan
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox