How to extract all sub-matrices of a bigger one - matlab

I have a matrix say:
Q = [05 11 12 16 25;
17 18 02 07 10;
04 23 20 03 01;
24 21 19 14 09;
06 22 08 13 15]
I would like to list out all the possible 3x3 matrices. Some examples are:
11 12 16;
18 2 7;
23 20 3
and
5 11 12;
17 18 2;
4 23 20;
etc.. Basically all the possible 3-by-3 matrices.
How do I do it? I must use a for loop?

If you have the Image Processing Toolbox, you can use the function IM2COL:
subMats = im2col(Q,[3 3]);
Each column of subMats contains the elements of a 3-by-3 matrix extracted from Q. Each of these columns can be reshaped into a 3-by-3 matrix as follows:
Q1 = reshape(subMats(:,1),[3 3]); %# Reshape column 1 into a 3-by-3 matrix

I'm guessing this is homework (if not, please forgive me), so here are some hints.
Draw out the structure of your 5x5 matrix.
Start in the upper left and draw a 3x3 submatrix within that 5x5. What are the elements covered by that matrix?
Go to the upper right. What elements are covered there?
Now go to the lower left. What about there?
Do you see how to cover the whole thing?

Related

How to use a loop in this case?

I have 3 matrices,
test_1=[1 2 3; 4 5 6; 7 8 9; 10 11 12; 13 14 15; 16 17 18];
test_2=[16 18 15; 6 5 8; 2 9 5; 11 19 21; 22 28 30; 14 16 19];
test_3=[11 9 7; 8 22 17; 27 31 30; 28 26 40; 66 57 36; 82 21 19];
I want to save all the elements of first two rows as,
firstrow_test_1= test_1([1; 2],:);
firstrow_test_2= test_2([1; 2],:);
firstrow_test_3= test_3([1; 2],:);
These matrices will give the result of all the elements of first two rows of test_1,test_2,test_3.
Then I want to take the 3rd and 4th row of test_1,test_2 and test_3 and save the elements in another set of metrices.
secondrow_test_1= test_1([3; 4],:);
secondrow_test_2= test_2([3; 4],:);
secondrow_test_3= test_3([3; 4],:);
Similarly for the rest, I have to do,
thirdrow_test_1= test_1([5; 6],:);
thirdrow_test_2= test_2([5; 6],:);
thirdrow_test_3= test_3([5; 6],:);
If I do this way then the code will be very long. So is there any way to write the code in a shorter way? Can anybody please help me to use a loop in this case,if it is possible?
What if we have 50 matrices like test_1,test_2,...,test_50 and we want to do the same in that case? (save the first 2 rows in another matrix,then the next 2 rows in another matrix..etc)
Thanks.
This is MatLab (Matrix Laboratory). you need to make use of multiple dimensions of a matrix to store the data instead of creating separate variables, if they all are of same dimensions (as in your case). Else if their dimensions are not same, you could use cell arrays.
Here are some links which might help you.
Multidimensional Arrays
Cell Arrays
Taking your example (see how easily you could solve your problem with use of nD matrices)
%// creating multidimensional matrix from those 2D matrix
A = cat(3,test_1, test_2, test_3); %// stacking each 2D slices in 3rd dimension
for example,
firstrow_test_1 could be accessed by using A(1:2,:,1).
Similarly,
firstrow_test_2 could be accessed by using A(1:2,:,2)
and so on.. I hope you got an idea.
Here i created a 3D matrix from the 2D slices as you had them as 2D slices. But you should be able to create 3D matrices directly and the same process could be applied.

Reshape column vector

Hello I'm working with MATLAB and I have a "z" column vector that has dimension of (9680 x 1). I want to reshape it in order to have an array "z" of dimension (44 x 220). I'm doing the following:
z=reshape(z,44,220);
I also tried:
z=reshape(z,[44,220]);
But the output is not right (at least the first row). I can see it by comparing the output matrix with the initial vector.
I just need the 220 first positions of the column vector to be the length of the first row of the matrix, then the next 220 positions of the vector to be the second row of the matrix and so on till obtaining 44 rows.
What am I doing wrong? Thanks for your help.
Matlab stores the matrix values in column major format (this is important during reshape). Since you want row major, you need to do
z = reshape(z, [220 44]).';
i.e. transpose afterwards.
I'd use Andreas H.'s approach.
As an alternative, there's a vec2mat function in the Communications Toolbox that does just that, and even fills missing values if needed:
>> x = 11:18;
>> vec2mat(x,4) %// no padding needed
ans =
11 12 13 14
15 16 17 18
>> vec2mat(x,5) %// padding needed; with 0 by default
ans =
11 12 13 14 15
16 17 18 0 0
>> vec2mat(x,5,-1) %// padding needed; with specified value
ans =
11 12 13 14 15
16 17 18 -1 -1

how to extract elements of a sparse matrix?

I have a sparse matrix:
A=
(14,13) 0.5286
(15,14) 0.6781
(16,15) 0.5683
(17,16) 1.2773
(18,17) 1.0502
(19,18) 0.4966
(21,19) 0.9951
(21,20) 0.4522
(22,21) 0.8507
(23,22) 1.0727
(24,23) 0.8288
(25,24) 0.5811
(26,25) 0.8235
(28,26) 1.5128
(30,28) 0.7966
(30,29) 0.6363
(31,29) 0.8254
(32,31) 0.8573
(33,32) 1.0753
that is result of a minimum spanning tree. now I want to extract 13,14,15,...26,28,29,...33.
as seen 27 is not between numbers. so pred give: 13 14 15 16 17 18 19 21 22 23 24 25 26 28 29 30 31 32 that 20 and 33 is not.
how can I extract total of numbers that say in top?
[ii jj] = find(A);
answer = unique([ii(:); jj(:)]);
should do it.
Note that the find command with two outputs gives you the row and column index of all nonzero elements. Since you have a minimum spanning tree, each number you care about needs to occur at least once in the row or column (for example your matrix never has the number 29 in the first index, but it occurs in the second).
The unique function makes sure that each number that occurs is only represented once.

how to find the middle elements of sub matrices in a matrix

I have a matrix and i want to consider it has 4 sub matrices which are placed together. How can I find the middle element of each sub matrix when they are together?
consider the matrix below. It is built by 4 sub matrices.
1 2 3 4 5 6
7 8 9 10 11 12
13 14 15 16 17 18
19 20 21 22 23 24
25 26 27 28 29 30
31 32 33 34 35 36
I want to get their middle elements so i could have:
8, 11, 26, 29
From what I have understood this might work for you and this is a demo, so use your own parameters -
Code
%%// Input matrix
A = rand(44,44);
%%/ Number of submatrices needed
num_submat = 16;%%// 4 for your example case
%%/ Number of submatrices along row and column
num_submat1= sqrt(num_submat);
%%// Middle element indices along each direction
v1 = floor(size(A,2)/(2*num_submat1))+1:size(A,2)/(num_submat1):size(A,2);
%%// Middle elements
middle_ele = A(v1,v1)
It is always helpful to know that matrix indexing in matlab goes columnwise eg,
indOrd = [1,4,7;2,5,8;3,6,9]
where the number is the index order and not related to your example. indOrd(4) would return 4. Try to use this to find the index locations.
Assuming that each submatrix has odd size 2n+1, the coordinates of the center of one submatrix alone are [n+1, n+1]. If your have a square with M*M submatrices (M=2 in your case), the coordinates are [n+1+i*(2*n+1), n+1+j*(2*n+1)], i and j taken independently in the range 0:M-1.
Turning back to Matlab, it is now quite easy to generate all indices of the centers of the submatrices grouped in the matrix A:
n = floor(size(A,1)/(2*M));
xc = n+1+reshape(repmat(0:M-1,M,1),[],1);
yc = n+1+reshape(repmat((0:M-1)',1,M),[],1);
centers = A(yc, xc);
For even-sized submatrix, you have to choose which element is the center, the modification is quite easy to do then.

Finding index of vector from its original matrix

I have a matrix of 2d lets assume the values of the matrix
a =
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
17 24 1 8 15
11 18 25 2 9
This matrix is going to be divided into three different matrices randomly let say
b =
17 24 1 8 15
23 5 7 14 16
c =
4 6 13 20 22
11 18 25 2 9
d =
10 12 19 21 3
17 24 1 8 15
How can i know the index of the vectors in matrix d for example in the original matrix a,note that the values of the matrix can be duplicated.
for example if i want to know the index of {10 12 19 21 3} in matrix a?
or the index of {17 24 1 8 15} in matrix a,but for this one should return only on index value?
I would appreciate it so much if you can help me with this. Thank you in advance
You can use ismember with the 'rows' option. For example:
tf = ismember(a, c, 'rows')
Should produce:
tf =
0
0
1
0
0
1
To get the indices of the rows, you can apply find on the result of ismember (note that it's redundant if you're planning to use this vector for matrix indexing). Here find(tf) return the vector [3; 6].
If you want to know the number of the row in matrix a that matches a single vector, you either use the method explained and apply find, or use the second output parameter of ismember. For example:
[tf, loc] = ismember(a, [10 12 19 21 3], 'rows')
returns loc = 4 for your example. Note that here a is the second parameter, so that the output variable loc would hold a meaningful result.
Handling floating-point numbers
If your data contains floating point numbers, The ismember approach is going to fail because floating-point comparisons are inaccurate. Here's a shorter variant of Amro's solution:
x = reshape(c', size(c, 2), 1, []);
tf = any(all(abs(bsxfun(#minus, a', x)) < eps), 3)';
Essentially this is a one-liner, but I've split it into two commands for clarity:
x is the target rows to be searched, concatenated along the third dimension.
bsxfun subtracts each row in turn from all rows of a, and the magnitude of the result is compared to some small threshold value (e.g eps). If all elements in a row fall below it, mark this row as "1".
It depends on how you build those divided matrices. For example:
a = magic(5);
d = a([2 1 2 3],:);
then the matching rows are obviously: 2 1 2 3
EDIT:
Let me expand on the idea of using ismember shown by #EitanT to handle floating-point comparisons:
tf = any(cell2mat(arrayfun(#(i) all(abs(bsxfun(#minus, a, d(i,:)))<1e-9,2), ...
1:size(d,1), 'UniformOutput',false)), 2)
not pretty but works :) This would be necessary for comparisons such as: 0.1*3 == 0.3
(basically it compares each row of d against all rows of a using an absolute difference)