How to create a random 3D matrix? - matlab

Is there any way of creating a 3D matrix randomly? There are ways to create random 2D matrices using randint function. Is there any inbuilt function like that?
E.g. a 4x4 matrix can be generated easily by using the randint function. What if I want to create a matrix of dimension 4x4x3?

You can use randi(imax, size1, size2, size3) function where imax refers to maximum of random integer values (mean upper bound) and 1 is lower bound. You can expand size argument to sizeN what you want.
This is an example of its usage:
>> A = randi(5, 4, 4, 3)
A(:,:,1) =
4 4 5 4
4 1 2 2
2 1 3 3
4 3 2 4
A(:,:,2) =
5 1 5 1
5 2 2 2
3 5 5 4
1 2 2 3
A(:,:,3) =
2 5 2 3
5 2 3 4
3 4 1 5
3 4 1 1

If you read the help carefully, you will notice that the randi function accepts any number of dimensions. You may do randi(10,3,3,3)
randi(10,3,3,3)
ans(:,:,1) =
9 10 3
10 7 6
2 1 10
ans(:,:,2) =
10 10 2
2 5 5
10 9 10
ans(:,:,3) =
8 1 7
10 9 8
7 10 8

Related

MATLAB - generating vector with sequence of values

Given two parameters:
n %number of repetitions per value
k %max value to repeat
I would like to create a vector of size n*k, which is a concatenation of k vectors of size n, such that the i'th vector contains the value i at each coordinate.
Example:
n = 5;
k = 9;
Desired result:
[1,1,1,1,1,2,2,2,2,2,3,3,3,3,3,4,4,4,4,4,5,5,5,5,5,6,6,6,6,6,7,7,7,7,7,8,8,8,8,8,9,9,9,9,9];
Is there an elegant way to achieve this?
Thanks!
quite a few ways to do it:
method 1:
A=1:k
repelem(A',n,1)'
method 2:
A=1:k
kron(A', ones(n,1))'
method 3:
A=1:k
B=repmat(A, n, 1)
B(:)'
method 4:
A=1:k
B=ones(n,1)*A
B(:)'
Here is an alternative method
A = reshape(mtimes((1:k).',ones(1,n)).',1,n*k)
A =
Columns 1 through 22
1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 4 4 5 5
Columns 23 through 44
5 5 5 6 6 6 6 6 7 7 7 7 7 8 8 8 8 8 9 9 9 9
Column 45
9
It multiplies each element by ones n times
>> mtimes((1:k).',ones(1,5)).'
ans =
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
and then reshapes the whole matrix to one vector

Reshape matrix from 3d to 2d keeping specific order

I would like to resize a matrix under the form let's say 4x3x5, to a 2d matrix of 20x3 but while preserving the order as illustrated below :
The function reshape() does not keep this particular order, how could I achieve this the simplest way possible ?
Let's solve these problems of concatenating and cutting across the third dimension once and for all!
Part I (3D to 2D) : Concatenate along the columns and across the 3rd dim of a 3D array, A to form a 2D array -
reshape(permute(A,[1 3 2]),[],size(A,2))
Part II (2D to 3D) : Cut a 2D array B after every N rows to form 3D slices of a 3D array -
permute(reshape(B,N,size(B,1)/N,[]),[1 3 2])
Sample run -
Part I (3D to 2D)
>> A
A(:,:,1) =
4 1 4 3
8 4 6 4
8 5 6 1
A(:,:,2) =
9 4 4 1
2 2 9 7
1 5 9 3
A(:,:,3) =
4 4 7 7
5 9 6 6
9 3 5 2
>> B = reshape(permute(A,[1 3 2]),[],size(A,2));
>> B
B =
4 1 4 3
8 4 6 4
8 5 6 1
9 4 4 1
2 2 9 7
1 5 9 3
4 4 7 7
5 9 6 6
9 3 5 2
Part II (2D to 3D)
>> N = 3;
>> permute(reshape(B,N,size(B,1)/N,[]),[1 3 2])
ans(:,:,1) =
4 1 4 3
8 4 6 4
8 5 6 1
ans(:,:,2) =
9 4 4 1
2 2 9 7
1 5 9 3
ans(:,:,3) =
4 4 7 7
5 9 6 6
9 3 5 2

Given a number, Generate a series of 'L' shaped matrix with MATLAB

Given any number. Lets say for example 5, I need to generate a matrix similar to this:
1 2 3 4 5
2 2 3 4 5
3 3 3 4 5
4 4 4 4 5
5 5 5 5 5
How to generate a matrix similar to this using Matlab?
I'd use bsxfun:
n = 5;
matrix = bsxfun(#max, 1:n, (1:n).');
An alternative (probably slower) is to use ndgrid:
n = 5;
[ii, jj] = ndgrid(1:n);
matrix = max(ii, jj);
Nothing will ever beat bsxfun as used by Luis Mendo., but for the sake of reminding people of the existence of Matlab's gallery function, here another approach:
n = 5;
A = gallery('minij',n)
B = n + 1 - A(end:-1:1,end:-1:1)
A =
1 1 1 1 1
1 2 2 2 2
1 2 3 3 3
1 2 3 4 4
1 2 3 4 5
B =
1 2 3 4 5
2 2 3 4 5
3 3 3 4 5
4 4 4 4 5
5 5 5 5 5

How to duplicate elements of a matrix without using the repmat function

Given the matrix I = [1,2;3,4], I would like to duplicate the elements to create a matrix I2 such that:
I2 = [1 1 1 2 2 2
1 1 1 2 2 2
1 1 1 2 2 2
3 3 3 4 4 4
3 3 3 4 4 4
3 3 3 4 4 4]
Other than using repmat, what other methods or functions are available?
Use kron:
>> N = 3 %// Number of times to replicate a number in each dimension
>> I = [1,2;3,4];
>> kron(I, ones(N))
ans =
1 1 1 2 2 2
1 1 1 2 2 2
1 1 1 2 2 2
3 3 3 4 4 4
3 3 3 4 4 4
3 3 3 4 4 4
This probably deserves some explanation in case you're not aware of what kron does. kron stands for the Kronecker Tensor Product. kron between two matrices A of size m x n and B of size p x q creates an output matrix of size mp x nq such that:
Therefore, for each coefficient in A, we take this value, multiply it with every value in the matrix B and we position these matrices in the same order as we see in A. As such, if we let A = I, and B be the 3 x 3 matrix full of ones, you thus get the above result.
Using indexing:
I = [1, 2; 3, 4]; %// original matrix
n = 3; %// repetition factor
I2 = I(ceil(1/n:1/n:size(I,1)), ceil(1/n:1/n:size(I,2))); %// result
One-liner with bsxfun -
R = 3; %// Number of replications
I2 = reshape(bsxfun(#plus,permute(I,[3 1 4 2]),zeros(R,1,R)),R*size(I,1),[])
Sample run -
I =
3 2 5
9 8 9
I2 =
3 3 3 2 2 2 5 5 5
3 3 3 2 2 2 5 5 5
3 3 3 2 2 2 5 5 5
9 9 9 8 8 8 9 9 9
9 9 9 8 8 8 9 9 9
9 9 9 8 8 8 9 9 9

Summation of matrices

I want to sum together each cell in the same position for each matrix. I have k amount of (i,j) matrices stored in MATLAB as (i,j,k) and I want to create one matrix which is the sum of all them - however the MATLAB command sums together every value in each column whereas I want to sum together each cell in the same position from each matrix.
1 3 4 3 4 0 2 4 4
0 3 1 2 7 8 0 3 1
9 0 2 0 1 2 1 2 3
I want to create a matrix that is:
1+3+2 3+4+4 4+0+4
0+2+1 3+7+3 1+8+1
9+0+1 0+1+2 2+2+3
=
6 11 8
3 13 10
10 3 7
Use a second input to sum specifying the dimension along which to sum (in your case, 3):
>> A(:,:,1) = [ 1 3 4
0 3 1
9 0 2 ];
>> A(:,:,2) = [ 3 4 0
2 7 8
0 1 2 ];
>> A(:,:,3) = [ 2 4 4
0 3 1
1 2 3 ];
>> sum(A,3)
ans =
6 11 8
2 13 10
10 3 7