Chances are you mean < instead of <= in your for loops.

Answer from Fraser on Stack Overflow
🌐
Dataquest Community
community.dataquest.io › q&a › dq courses
Why does matrix multiplication with a 1D-array work? - DQ Courses - Dataquest Community
July 1, 2019 - a = np.array([[1,1], [2,2], [3,3]]) b = np.array([10,20]) a.shape b.shape a@b Output: (3, 2) (2,) array([30, 60, 90]) Something must be happening to b in the backend? I expected @ to require …
🌐
DaniWeb
daniweb.com › programming › software-development › threads › 120915 › multyplying-one-dimensional-dynamic-arrays-matrix
c++ - Multyplying one dimensional dynamic arrays(matrix) [SOLVED] | DaniWeb
April 26, 2008 - Rename the dimensions to M x K for A and K x N for B. Then each C[i,j] is the dot product of row i of A and column j of B. The trick in a flat array is that a column of B is not contiguous: start at B + j and advance by N each step.
🌐
Cprogramming
cboard.cprogramming.com › cplusplus-programming › 39928-matrix-multiplication-using-1-d-arrays.html
Matrix multiplication using 1-d arrays
May 25, 2003 - << endl; int colCount = 0, rowInc = 0, colInc = 0, Mat1Size = mList[MatrixOne].Row * mList[MatrixOne].Col, Mat2Size = mList[MatrixTwo].Row * mList[MatrixTwo].Col; Matrix Answer; //creates answer matrix Answer.Row = mList[MatrixOne].Row; Answer.Col = mList[MatrixTwo].Col; Answer.Data = new int[Answer.Row*Answer.Col]; //allocates matrix space for (int ii = 0; ii < Answer.Row*Answer.Col; ii++) { Answer.Data[ii] = 0; //initializes all data content to zero } for (int Count = 0; Count < Answer.Row*Answer.Col; Count++) { colCount = 0; colInc = 0; for (int rowCount = rowInc; rowCount < mList[MatrixOne
Top answer
1 of 2
12

No. Let's first look at it as two dimensional arrays, so we know what we're talking about:

int i[4][4];
int j[4][4];
int k[4][4];

for (int x = 0; x < 4; x++) { // row number of output
    for (int y = 0; y < 4; y++) { // column number of output
        k[x][y] = 0;
        for (int z = 0; z < 4; z++) { // four elements are added for this output
            k[x][y] += i[x][z] * j[z][y];
        }
    }
}

Important difference!!! We start at 0 for the indices!

Now I'll convert it to int[16]s for you assuming that i[1] is the second element of the first row:

int i[16];
int j[16];
int k[16];

for (int x = 0; x < 4; x++) { // row number of output
    for (int y = 0; y < 4; y++) { // column number of output
        k[4*x+y] = 0;
        for (int z = 0; z < 4; z++) { // four elements are added for this output
            k[4*x+y] += i[4*x+z] * j[4*z+y];
        }
    }
}

This is equivalent to your code except for the fact that you start at 1 instead of zero.

2 of 2
2

@Dan "equivalent ... except .. you start at 1 instead of zero" solves the OP's first problem, but not the OP's second problem.

The OP's second and larger problem is functioning 3x3 matrix multiplier code that reads enough like buggy 4X4 matrix multiplier code to trip up its reviewers. I suspect the problems STARTED with the common mistake of using 1-based counting instead of 0-based indexing. And that was followed by a superficial solution: just pad the indexed vectors so that the 1-based counts are still in range, never mind the unused gaps.

The reviewer sees the idiomatic i*4+j and thinks "AHA! 4 MUST be the matrix dimension." implicitly reasoning "...because otherwise there would be unused gaps".

This miscue can be pinned on the use of "magic numbers", namely 3 and 4. Which of these is the intended matrix dimension? If the magic numbers had been referenced by name/purpose rather than by value, like via

    const unsigned int DIMENSION = 3;
    const unsigned int ROW_LENGTH = DIMENSION + 1;

the intent would have been clear and the mis-step of padding the vectors much easier to spot.

Or maybe I'm completely wrong and it always was just buggy 4x4 matrix multiplier code that coincidentally gave the right answer for 3x3 matrices.

Regardless, the common wisdom goes: Unless there is a very good reason to do otherwise,

  • use 0-based indexing, because it's simple, efficient, and idiomatic.

  • likewise, use the for loop < idiom for 0-based index iteration:

    for (unsigned int i = 0; i < DIMENSION; i++) {
    
  • reference special numbers in the problem domain via named constants, for self-documentation (as well as for flexibility).

🌐
Unisa
lo.unisa.edu.au › mod › book › view.php
Practical 3: Arrays: One-Dimensional Operations | learnonline
MATLAB has a specific array or ‘element-by-element’ multiplication, •*, designed for one-dimensional arrays, that is also called ‘Hadamard product’ in mathematics, and uses a dot, •, before multiply sign, *, eg V1•*V2
Find elsewhere
🌐
Codecademy
codecademy.com › article › numpy-matrix-multiplication-a-beginners-guide
NumPy Dot Product and Matrix Multiplication: Complete Guide | Codecademy
np.matmul() is strictly for matrix multiplication, supports batch operations, treats 1D arrays as 1xn and nx1, and has the @ operator equivalent.
🌐
Scribd
scribd.com › document › 861233263 › 1-1D-2D-array
Matrix Operations with 1D and 2D Arrays | PDF | Matrix (Mathematics) | Matrix Theory
Both the 1D and 2D array implementations of matrix multiplication share the same computational complexity, generally O(m * n * p) for multiplying an m x n matrix with an n x p matrix.
🌐
TutorialsPoint
tutorialspoint.com › matrix-product-of-a-2d-first-argument-and-a-1d-array-second-argument-in-numpy
numpy.matmul()
On the other hand, if either argument is 1-D array, it is promoted to a matrix by appending a 1 to its dimension, which is removed after multiplication.
🌐
Math is Fun
mathsisfun.com › algebra › matrix-multiplying.html
How to Multiply Matrices
A Matrix is an array of numbers: A Matrix (This one has 2 Rows and 3 Columns). To multiply a matrix by a single number, we multiply it by every...
🌐
Devx
forums.devx.com › showthread.php
Matrix multiplication using 1Dimensional arrays
March 30, 2005 - Well you can hack on it... the malloc/free thing should probably use some sort of allocated (passed in, or whatever) swap space for speed, you can do calloc instead to zero the memory, but this covers the technique pretty well even if its sloppy C code. double*multiply(double *a,int ar,int ac,double*b,int br,int bc) /*ar = a's rows, ac = a's cols, etc*/ {/*returns null for bad dimensions and ab for good ones.*/ double *product; int i,j,k,dx; if(ac != br)/*then its not valid.*/ return(NULL); product = (double*)malloc(sizeof(double)*ar*bc); memset(product,0,sizeof(double)*ar*bc); /*set up zeros
🌐
ScienceDirect
sciencedirect.com › topics › computer-science › matrix-multiplication
Matrix Multiplication - an overview | ScienceDirect Topics
Each iteration of the loop accesses ... Pvalue (line 08). Let us first focus on accessing the M element within the for-loop. M is linearized into an equivalent 1D array using row-major order....
🌐
Wikipedia
en.wikipedia.org › wiki › Matrix_multiplication
Matrix multiplication - Wikipedia
January 27, 2026 - In mathematics, specifically in ... two matrices. For matrix multiplication, the number of columns in the first matrix must be equal to the number of rows in the second matrix....
🌐
Programiz
programiz.com › c-programming › examples › matrix-multiplication
C Program to Multiply Two Matrices Using Multi-dimensional Arrays
The user first enters the dimensions (r1, c1, r2, c2) and elements of each matrix, ensuring that the number of columns in the first matrix matches the number of rows in the second matrix. Finally, the multiplyMatrices function then multiplies the matrices by iterating through their elements, storing the results in the result[][] matrix.
🌐
Reshish
matrix.reshish.com › matrix-multiplication
Matrix Multiplication Calculator
The main condition of matrix multiplication is that the number of columns of the 1st matrix must equal to the number of rows of the 2nd one.
🌐
DigitalOcean
digitalocean.com › community › tutorials › numpy-matrix-multiplication
NumPy Matrix Multiplication: Methods and Examples | DigitalOcean
August 4, 2022 - If you want element-wise matrix multiplication, you can use multiply() function. import numpy as np arr1 = np.array([[1, 2], [3, 4]]) arr2 = np.array([[5, 6], [7, 8]]) arr_result = np.multiply(arr1, arr2) print(arr_result)
🌐
GeeksforGeeks
geeksforgeeks.org › python › numpy-multiply-2d-array-to-1d-array
NumPy | Multiply 2D Array to 1D Array - GeeksforGeeks
July 11, 2025 - The np.newaxis() method of the NumPy library allows us to increase the dimension of an array by 1 dimension. We use this method to perform element-wise multiplication by reshaping the 1D array to have a second-dimension