 Tags IOS SQL HTML C RUBY-ON-RAILS MYSQL ASP.NET DEVELOPMENT RUBY .NET LINUX SQL-SERVER REGEX WINDOWS ALGORITHM ECLIPSE VISUAL-STUDIO STRING SVN PERFORMANCE APACHE-FLEX UNIT-TESTING SECURITY LINQ UNIX MATH EMAIL OOP LANGUAGE-AGNOSTIC VB6 # Create matrix from smaller matrices in R

By : DANNY
Date : September 17 2020, 06:00 AM
I wish this help you Is there a general function to build a matrix from smaller blocks, i.e. build matrix , Here are some base R solutions. Maybe you can use code :
``````M <- rbind(cbind(A,B),cbind(C,D))
``````
``````u <- list(list(A,B),list(C,D))
M <- do.call(rbind,Map(function(x) do.call(cbind,x),u))
``````
``````A <- matrix(1:4,nrow = 2)
B <- matrix(1:6,nrow = 2)
C <- matrix(1:6,ncol = 2)
D <- matrix(1:9,nrow = 3)
``````
``````> M
[,1] [,2] [,3] [,4] [,5]
[1,]    1    3    1    3    5
[2,]    2    4    2    4    6
[3,]    1    4    1    4    7
[4,]    2    5    2    5    8
[5,]    3    6    3    6    9
``````

## Split a matrix into smaller matrices

By : user6718461
Date : March 29 2020, 07:55 AM
hop of those help? I have a matrix of size 20*20. I want to split it into two sub-matrices of size 20*10. If i use the following code: , do the following
code :
``````f = mat2cell( e, 20, [10 10] );
``````

## Create a larger matrix from smaller matrices in numpy

By : Mehdi Majidi
Date : March 29 2020, 07:55 AM
hop of those help? I have 3 matrices A,B,C. I wish to create a larger matrix of the form , This:
code :
``````numpy.bmat([[numpy.zeros(appropriate_shape), A], [B, C]])
``````

## Algorithm to create a square matrix given any number of smaller square matrices

By : rng
Date : March 29 2020, 07:55 AM
it helps some times I will want to plot some images using Opencv, and for this I would like to glue images together. , Here's a snippet that I use for doing this sort of thing:
code :
``````import numpy as np

def montage(imgarray, nrows=None, border=5, border_val=np.nan):
"""
Returns an array of regularly spaced images in a regular grid, separated
by a border

imgarray:
3D array of 2D images (n_images, rows, cols)
nrows:
the number of rows of images in the output array. if
unspecified, nrows = ceil(sqrt(n_images))
border:
the border size separating images (px)
border_val:
the value of the border regions of the output array (np.nan
renders as transparent with imshow)
"""

dims = (imgarray.shape, imgarray.shape+2*border,
imgarray.shape + 2*border)

X = np.ones(dims, dtype=imgarray.dtype) * border_val
X[:,border:-border,border:-border] = imgarray

# array dims should be [imageno,r,c]
count, m, n = X.shape

if nrows != None:
mm = nrows
nn = int(np.ceil(count/nrows))
else:
mm = int(np.ceil(np.sqrt(count)))
nn = mm

M = np.ones((nn * n, mm * m)) * np.nan

image_id = 0
for j in xrange(mm):
for k in xrange(nn):
if image_id >= count:
break
sliceM, sliceN = j * m, k * n
img = X[image_id,:, :].T
M[sliceN:(sliceN + n), sliceM:(sliceM + m)] = img
image_id += 1

return np.flipud(np.rot90(M))
``````
``````from scipy.misc import lena
from matplotlib import pyplot as plt

img = lena().astype(np.float32)
img -= img.min()
img /= img.max()
imgarray = np.sin(np.linspace(0, 2*np.pi, 25)[:, None, None] + img)

m = montage(imgarray)
plt.imshow(m, cmap=plt.cm.jet)
``````

## How can I create a matrix based on smaller matrices?

By : Giuseppe Diciolla
Date : March 29 2020, 07:55 AM
it should still fix some issue How about http://docs.scipy.org/doc/scipy-dev/reference/generated/scipy.sparse.vstack.html
If that's too slow, take the fast path from here: https://github.com/scipy/scipy/blob/master/scipy/sparse/construct.py#L396 (in future Scipy versions, vstack itself will be fast in this case).

## Numpy: create a matrix from smaller matrices

By : user33362
Date : March 29 2020, 07:55 AM
will be helpful for those in need We can use NumPy's Kronecker product -
code :
``````np.kron(np.ones((2, 2), dtype=int), m)
``````
``````In : m
Out:
array([[1, 2],
[3, 4]])

In : np.kron(np.ones((2, 2), dtype=int), m)
Out:
array([[1, 2, 1, 2],
[3, 4, 3, 4],
[1, 2, 1, 2],
[3, 4, 3, 4]])
`````` 