multiply a vector by a matrix line by line

APL-related discussions - a stream of APL consciousness.
Not sure where to start a discussion ? Here's the place to be
Forum rules
This forum is for discussing APL-related issues. If you think that the subject is off-topic, then the Chat forum is probably a better place for your thoughts !
Post Reply
BenoitM
Posts: 10
Joined: Tue Jan 12, 2021 3:04 pm

multiply a vector by a matrix line by line

Post by BenoitM »

I am trying to multiply the vector x by the matrix y line by line:
x
100 200 150 25 35
y
1 1 0 0 0
0 0 1 0 1
0 0 0 1 0

to get:
100 200 0 0 0
0 0 150 0 35
0 0 0 25 0

with x and y having any number of columns and y having any number of rows
I tried many thigs but cannot get how to do this...

Please help,
User avatar
Phil Last
Posts: 628
Joined: Thu Jun 18, 2009 6:29 pm
Location: Wessex

Re: multiply a vector by a matrix line by line

Post by Phil Last »

Your target result implies that you actually want the length of the vector and width of the matrix to conform which seems to be contradicted by the final sentence. However
x ×⍤1⊢ y
will give the answer.

corrected immediately PL
User avatar
Phil Last
Posts: 628
Joined: Thu Jun 18, 2009 6:29 pm
Location: Wessex

Re: multiply a vector by a matrix line by line

Post by Phil Last »

The rank operator applies its left function operand to cells of its arguments defined by the ranks in its right vector operand. so in this case 1-cells (rows) in each argument are paired. As there is only one 1-cell in x it is repeated for each in y
Veli-Matti
Posts: 94
Joined: Sat Nov 28, 2009 3:12 pm

Re: multiply a vector by a matrix line by line

Post by Veli-Matti »

..or just using another way:
x×[2]y
-Veli-Matti
BenoitM
Posts: 10
Joined: Tue Jan 12, 2021 3:04 pm

Re: multiply a vector by a matrix line by line

Post by BenoitM »

Thank you both!
Post Reply