I have a long nx3 matrix. For eg, I take a 9x3 matrix
A =
8 9 8
9 2 9
2 9 6
9 9 1
6 5 8
1 8 9
3 2 7
5 4 7
9 9 7
Now I want it reshaped, (taking successive 3x3 sub-matrix to the next dimension) such that,
out(:,:,1) =
8 9 8
9 2 9
2 9 6
out(:,:,2)
9 9 1
6 5 8
1 8 9
out(:,:,3)
3 2 7
5 4 7
9 9 7
I could do this with loops but I wanted to know how to vectorize this process..
Can i do it with reshape and permute alone?
Yes, you can use permute and reshape:
A = [...
8 9 8
9 2 9
2 9 6
9 9 1
6 5 8
1 8 9
3 2 7
5 4 7
9 9 7
3 2 7
5 4 7
9 9 7]
n = size(A,2);
B = permute( reshape(A.',n,n,[]), [2 1 3]) %'
%// or as suggested by Divakar
%// B = permute( reshape(A,n,n,[]), [1 3 2])
out(:,:,1) =
8 9 8
9 2 9
2 9 6
out(:,:,2) =
9 9 1
6 5 8
1 8 9
out(:,:,3) =
3 2 7
5 4 7
9 9 7
out(:,:,4) =
3 2 7
5 4 7
9 9 7
This could be one approach -
N = 3
out = permute(reshape(A,N,size(A,1)/N,[]),[1 3 2])
This one has the advantage of avoiding the transpose as used in the other answer by #thewaywewalk.
Sample run -
A =
8 9 8
9 2 9
2 9 6
9 9 1
6 5 8
1 8 9
3 2 7
5 4 7
9 9 7
out(:,:,1) =
8 9 8
9 2 9
2 9 6
out(:,:,2) =
9 9 1
6 5 8
1 8 9
out(:,:,3) =
3 2 7
5 4 7
9 9 7
Related
I have a matrix A ,and vector x as following (left side)
where S0, H0,...is row number of each block. I want to exchange these blocks such that S0 and S1; H0 and H1 are near together as right side. This is my code
S0=3;
H0=2;
N0=2;
S1=4;
H1=5;
N1=4;
Cols=5;
Rows=S0+H0+N0+S1+H1+N1;
A=randi(10,[ Rows Cols]);
x=randi(10,[Rows 1]);
%% Exchange two block
temp=A(S0+H0+1:S0+H0+N0,1:end);
A(S0+H0+1:S0+H0+H1,1:end)=A(S0+H0+N0+S1+1:S0+H0+N0+S1+H1,1:end);
A(S0+H0+N0+S1+1:S0+H0+N0+S1+H1,1:end)=temp;
%% How exchange x
The above code is not work. How can I fixed it in MATLAB? Thank in advance.
One approach with mat2cell and cell2mat -
grps = [S0,H0,N0,S1,H1,N1]
new_pattern = [1 4 2 5 3 6]
celldata_roworder = mat2cell((1:size(A,1))',grps); %//'
newx = cell2mat(celldata_roworder(new_pattern)).'; %//'
newA = A(newx,:)
Sample run -
Input :
A =
6 8 9 8 7
4 8 8 3 4
3 8 2 1 10
5 2 6 8 3
5 7 4 7 7
4 5 6 8 7
6 3 4 7 4
8 1 5 5 2
5 9 2 4 1
5 2 3 9 5
2 2 1 4 2
1 7 10 9 8
3 9 7 8 4
4 6 10 9 9
7 8 2 6 8
10 2 10 7 6
10 10 8 10 2
5 6 6 5 10
3 7 5 1 3
8 1 3 9 10
grps =
3 2 2 4 5 4
new_pattern =
1 4 2 5 3 6
Output:
newx =
1 2 3 8 9 10 11 4 5 12 ...
13 14 15 16 6 7 17 18 19 20
newA =
3 3 2 5 8
4 3 3 7 7
1 5 2 8 1
4 6 4 1 4
7 1 5 8 8
4 9 10 10 8
7 10 10 4 3
7 3 1 6 9
2 9 2 6 10
1 1 7 10 3
10 10 10 4 7
9 1 8 9 5
8 7 4 5 7
9 8 7 5 3
1 10 7 6 8
8 1 10 6 1
4 6 3 3 2
7 9 3 2 9
6 9 7 4 8
6 7 6 8 10
I assume you are using a 2-dimensional matrix with Row rows and Cols columns.
You can use the colon : as a second index to address a full row, e.g. for the third row:
A(3, :)
(equal to A(3, 1:end) but little bit clearer).
So you could split your matrix into lines and re-arrange them like this (putting back together the lines to a two-dimensional matrix):
A = [ A(3:4, :); A(1:2, :); A(5:end, :) ]
This moves rows 3 and 4 at the beginning, then old lines 1 and 2 and then all the rest. Does this help you?
Hint: you can use eye for experimenting.
I have a 3D matrix and I want to store each 2D component of this in the row of another 2D matrix which has many rows as the 3rd dimension of the 3D matrix.
How can I do this?
With permute & reshape -
reshape(permute(A,[3 2 1]),size(A,3),[])
Sample run -
>> A
A(:,:,1) =
7 1 7 5
3 4 8 5
9 4 2 6
A(:,:,2) =
7 7 2 4
7 6 5 6
3 2 9 3
A(:,:,3) =
7 7 5 3
3 9 2 8
5 9 2 3
>> reshape(permute(A,[3 2 1]),size(A,3),[])
ans =
7 1 7 5 3 4 8 5 9 4 2 6
7 7 2 4 7 6 5 6 3 2 9 3
7 7 5 3 3 9 2 8 5 9 2 3
If you don't mind a little indexing madness...
You can build a linear index with the appropriate shape, which applied on the original array will give the desired result:
B = A(bsxfun(#plus, (1:L*M:L*M*N).', reshape(bsxfun(#plus, (0:L:L*M-1).', 0:L-1),1,[])));
Example:
>> A = randi(10,2,3,4)-1; %// example array; size 2x3x4
>> A
A(:,:,1) =
5 3 2
9 8 9
A(:,:,2) =
8 7 4
9 8 6
A(:,:,3) =
3 4 8
0 4 4
A(:,:,4) =
2 8 8
4 6 7
Result:
>> B
B =
5 3 2 9 8 9
8 7 4 9 8 6
3 4 8 0 4 4
2 8 8 4 6 7
That is easily done with MATLABs matrix unrolling syntax:
A=ones(N,M,O);
B=zeros(O,N*M);
for ii=1:size(A,3)
aux=A(:,:,ii); % aux is NxM
B(ii,:)=aux(:); % unroll!
end
(note I called O the thing you call N in your pictures)
I'm working on MATLAB. I have the following matrices
A = [
1 2 3 4
5 6 7 8
1 5 2 3
6 7 8 9
1 3 6 2
6 3 1 6
9 7 4 7
];
B = [
1 5 2 3
6 7 8 9
];
I want to find A-B
so that the answer should be like,
ans = [
1 2 3 4
5 6 7 8
1 3 6 2
6 3 1 6
9 7 4 7
];
Use setdiff with the 'rows' and 'stable' options:
>> C = setdiff(A,B,'rows','stable')
C =
1 2 3 4
5 6 7 8
1 3 6 2
6 3 1 6
9 7 4 7
Use ismember to find the common rows and neglect those in the final output.
Code
out = A(~ismember(A,B,'rows'),:)
Output
out =
1 2 3 4
5 6 7 8
1 3 6 2
6 3 1 6
9 7 4 7
clear;
s=0;
A = [
1 2 3 4
5 6 7 8
1 5 2 3
6 7 8 9
1 3 6 2
6 3 1 6
9 7 4 7
];
B = [
1 5 2 3
6 7 8 9
];
for i=1:size(B)
s=s+(ismember(A, B(i,:), 'rows'))
end
A_B = A(s==0,:)
#Divakar or #chappjc's answers are the way to go.
But I can't help inviting bsxfun to the party:
C = A(~any(squeeze(all(bsxfun(#eq, A.', permute(B, [2 3 1])))).'),:);
And its friend pdist2 is coming too:
C = A(all(pdist2(A, B, 'hamming').'),:);
I have random matrix(A) and I find a result I'd like to use later for my code
A=randint(5,7,[1,9])
ans A =
8 1 2 2 6 7 7
9 3 9 4 1 7 1
2 5 9 9 8 4 3
9 9 5 8 9 6 1
6 9 8 9 7 2 1
How can I now get:
A = [8,1,2,2,6,7,7;9,3,9...7,2,1];
without having to type it myself.
MATLAB has a function for that: MAT2STR
>> A = randi([1,9],[5,7]);
>> mat2str(A)
ans =
[5 5 7 5 3 2 5;5 6 5 3 8 4 1;9 8 8 1 7 9 6;1 5 5 1 8 6 3;3 4 5 8 9 9 5]
This is suitable for use with EVAL
Just thought of another way. Your goal is to have A in your script - right?
You can just paste it as follows:
A = [
8 1 2 2 6 7 7
9 3 9 4 1 7 1
2 5 9 9 8 4 3
9 9 5 8 9 6 1
6 9 8 9 7 2 1
]
(note the square brackets)
It will evaluate to your original matrix.
Make the string yourself:
Str = ['[' sprintf('%i',A(1)) sprintf(',%i',A(2:end)) ']']
Note this string does not contain any ; as in your example. so when you evaluate it you will get a 1x35 vector (instead of the original 5x7matrix)
So easiest way to fix this would be to add after you evaluate the string.
A = reshape(A,5,7)
It will look like
B = [....
B = reshape(B,5,7)
I am trying to create a matrix that is 3 x n, with each of the columns being the same. What's the easiest way of achieving it? Concatenation?
After
n=7
x=[1;2;3]
it's either
repmat(x,[1 n])
or
x(:,ones(1,n))
(Octave can be considered as an open source/free version of MATLAB)
octave-3.0.3:2> rowvec = [1:10]
rowvec =
1 2 3 4 5 6 7 8 9 10
octave-3.0.3:3> [rowvec; rowvec; rowvec]
ans =
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
Use repmat if the number of rows is large.
octave-3.0.3:7> repmat(rowvec, 10, 1)
ans =
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
Use multiplication with a 1 x 3 matrix of ones
eg, x * [1 1 1]
Edit:
In Octave:
octave-3.0.3.exe:1> x = [1;2;3;4]
x =
1
2
3
4
octave-3.0.3.exe:5> x * [1 1 1]
ans =
1 1 1
2 2 2
3 3 3
4 4 4