# [racket] How to iterate through the arrays in a matrix?

 From: Jens Axel Søgaard (jensaxel at soegaard.net) Date: Fri Nov 28 17:47:35 EST 2014 Previous message: [racket] How to iterate through the arrays in a matrix? Next message: [racket] How to iterate through the arrays in a matrix? Messages sorted by: [date] [thread] [subject] [author]

```Hi,

Using high level constructs:

#lang racket
(require math/matrix)

(define (centering-matrix n)
(define  1/n (/ 1 n))
(define -1/n (- 1/n))
(define 1-1/n (- 1 1/n))
(build-matrix n n (λ (i j) (if (= i j) 1-1/n -1/n))))

(define (scatter-matrix X)
(define n (matrix-num-cols X))
(define C (centering-matrix n))
(define XT (matrix-transpose X))
(matrix* X C XT))

(scatter-matrix (matrix [[1 2 3] [4 5 6]]))

2014-11-28 23:22 GMT+01:00 Animesh Pandey <animeshpandey.etno at gmail.com>:
> Hi,
> This explanation might be of some help.
>
> What I am trying to find is the Scatter Matrix of a given matrix
> "final-data".
> Since "final-data" is a list of lists and not a matrix I wrote the following
> functions:
>
> ;; l is a list
> ;; MAT-COL is 4
> (define (sub-vec l)
>   (matrix-
>    ;; l is converted to a matrix for matrix subtraction
>    (list->matrix 1 MAT-COL l)
>    ;; MEAN is a constant array/matrix
>    (list->matrix 1 MAT-COL MEAN)))
>
> ;; computes the product of the result of "sub-vec" and its transpose which
> is a
> ;; (MAT-COL X MAT-COL) dimension matrix for each iteration and then sums up
> ;; all the matrices in to one which is the scatter matrix
> (define scatter-matrix
>   (apply matrix+ (map
>                   (λ(l)
>                     (matrix* (matrix-transpose (sub-vec l)) (sub-vec l)))
>                   final-data)))
>
> I am sorry I really don't have any examples for this.
>
>
> On Fri, Nov 28, 2014 at 5:10 PM, Daniel Prager <daniel.a.prager at gmail.com>
> wrote:
>>
>> Could you please give a few examples, with inputs and desire outputs?
>>
>> Dan
>>
>> On Nov 29, 2014 8:57 AM, "Animesh Pandey" <animeshpandey.etno at gmail.com>
>> wrote:
>>>
>>> (map
>>>  (λ(i)
>>>    (matrix-
>>>     (list->matrix 1 4 i)
>>>     (list->matrix 1 4 arr)))
>>>  final-data)
>>>
>>> I used this method where final-data is a list of lists and I iterate
>>> through each list, convert it to a matrix and then subtract a pre-defined
>>> constant list to it. What I really need is a way to iterate through a matrix
>>> and not a list of lists as I have do to a lot of matrix operations.
>>>
>>>
>>> On Fri, Nov 28, 2014 at 4:41 PM, Animesh Pandey
>>> <animeshpandey.etno at gmail.com> wrote:
>>>>
>>>> Hi,
>>>> I have a 80 X 4 matrix. I have iterate through each row and subtract
>>>> each row with another array at each iteration. At first I thought to convert
>>>> all array to list and do all the operations and then again convert the final
>>>> list if lists to a matrix. But I somehow think that is not a suited way. I
>>>> tried using "for*/matrix" but could not get it right. Could any one give an
>>>> example on how to iterate a matrix (array for arrays)?
>>>>
>>>> My matrix looks like this:
>>>> > (list*->matrix final-data)
>>>> (array
>>>>  #[#[5.1 3.5 1.4 0.2]
>>>>    #[4.9 3.0 1.4 0.2]
>>>>    #[4.7 3.2 1.3 0.2]
>>>>    #[4.6 3.1 1.5 0.2]
>>>>    #[5.0 3.6 1.4 0.2]
>>>>    #[5.4 3.9 1.7 0.4]
>>>>    #[4.6 3.4 1.4 0.3]
>>>>    #[5.0 3.4 1.5 0.2]
>>>>    #[4.4 2.9 1.4 0.2]
>>>>    #[4.9 3.1 1.5 0.1]
>>>> ...........
>>>>
>>>> Thanks!
>>>
>>>
>>>
>>> ____________________
>>>   Racket Users list:
>>>   http://lists.racket-lang.org/users
>>>
>
>
> ____________________
>   Racket Users list:
>   http://lists.racket-lang.org/users
>

--
--
Jens Axel Søgaard

```

 Posted on the users mailing list. Previous message: [racket] How to iterate through the arrays in a matrix? Next message: [racket] How to iterate through the arrays in a matrix? Messages sorted by: [date] [thread] [subject] [author]