I have 2 matrices,
a= [1 2 3; 4 5 6; 7 8 9; 10 11 12];
and
b= [13 14 15; 16 17 18; 19 20 21; 22 23 24];
How can I make the average of these 2 matrices and store the values in another matrix "C" on Matlab?
The value of C will be,
c= [(1+13)/2 (2+14)/2 (3+15)/2; (4+16)/2 (5+17)/2 (6+18)/2;...]
Thanks.
You would do:
c = (a+b)/2
This will give you the desired result.
Another way would be to stack the matrices on top of each other in 3D, then find the average along the third dimension:
c = mean(cat(3, a, b), 3);
Related
Assuming we have 3 matrices, A, B and C, all are of the same size 256x256. It is known that last 20% of columns of Matrix A is identical to first 20% of Matrix B and last 10% of Matrix B is identical to first 10% of Matrix C. So in these cases since we know the overlapping amount, I do not need to compare the 3 matrices, but i want to join them at the overlap.
Taking a smaller Matrix as an example here are the 3 Matrices
A = [1 2 3 4 ; 5 6 7 8; 9 10 11 12];
B = [3 4 13 14; 7 8 15 16; 11 12 17 18];
C = [14 19 20 21; 16 22 23 24; 18 25 26 27];
So I would like my output to be
D = [1 2 3 4 13 14 19 20 21
5 6 7 8 15 16 22 23 24
9 10 11 12 17 18 25 26 27
I hope this might explain it better. I am extremely new to matlab. I tried using matrix shift, but we have only circular shift available.
Concatenation does not work because it just joins the 3 matrices. What would be the best way to overlay these 3 matrices together ?
Make proper use of matrix indexing and concatenation
For your example
D = [A B(:,3) C];
For a 256x256 Matrix and your concatenation conditions:
D = [A B(:, 0.2*256+1 : 0.9*256) C]
Since 256/10 is no integer you may adjust the index values
As I am trying to multiply a m x n Matrix with a p-dimensional vector, I am stumbling across some difficulties.
Trying to avoid for loops, here is what I am looking to achieve
enter code here
M = [1 2 3; p = [1;2;3]
4 5 6;
7 8 9]
I want to obtain a 3x3x3 matrix, where the slices in third dimension are simply the entries of M multiplied by the respective entry in p.
Help is much appreciated
You can use bsxfun with permute for a vectorized (no-loop) approach like so -
out = bsxfun(#times,M,permute(p(:),[3 2 1]))
You would end up with -
out(:,:,1) =
1 2 3
4 5 6
7 8 9
out(:,:,2) =
2 4 6
8 10 12
14 16 18
out(:,:,3) =
3 6 9
12 15 18
21 24 27
With matrix-multiplication -
out = permute(reshape(reshape(M.',[],1)*p(:).',[size(M) numel(p)]),[2 1 3])
Need some help understanding the interp2.Here is example
[x y] = meshgrid([1:4],[1:4]);
l = [ 5 6 7 8;9 10 11 12;13 14 15 16; 17 18 19 20];
m = [ 1 2 3 4];
n = [2 3 4 5];
c = interp2(x,y,l,m,n);
How does 2d x y matrix getting interpolated over 1D m and n matrix. I will appreciate your help. Thank you
In your case, you are interpolating at the points (1,2) (2,3) (3,4) (4,5). First three are elements of your input, they are basically copied. The last one is not within your input range so it's NAN
I would like to transform the matrix A into the matrix B without using cells (e.g. mat2cell) in Matlab, where
A=[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];
B=[1 2 3 10 11 12 19 20 21;
4 5 6 13 14 15 22 23 24;
7 8 9 16 17 18 25 26 27];
All you need is some reshape + permute magic -
N = 3; %// Cut after every N rows and this looks like the no. of columns in A
B = reshape(permute(reshape(A,N,size(A,1)/N,[]),[1 3 2]),N,[])
This builds a linear index to rearrange the entries of A and then reshapes into the desired matrix B:
m = 3; %// cut size in rows of A. Assumed to divide size(A,1)
n = size(A,2);
p = size(A,1);
ind = bsxfun(#plus, ...
bsxfun(#plus, (1:m).', (0:n-1)*p), permute((0:p/m-1)*m, [1 3 2]));
B = reshape(A(ind(:)), m, [])
I have matrix A in Matlab of dimension hxk and a matrix B of dimension yxk. I want to construct a vector C of dimension yx1 listing in each row j how many times B(j,:) appears in A.
If you are looking for perfect matches, one solution with bsxfun -
C = squeeze(sum(all(bsxfun(#eq,A,permute(B,[3 2 1])),2),1))
You can also use pdist2 (from the Statistics Toolbox):
C = sum(pdist2(A, B)==0);
Another solution using ismember and accumarray
A=[1 2 3; 4 5 6; 7 8 9; 1 2 3; 4 5 6; 10 11 12; 7 8 9];
B=[1 2 3; 10 11 12; 3 4 5; 7 8 9];
[uB,aB,cB]=unique(B,'rows');
[~,LocB] = ismember(A,uB,'rows');
C = accumarray(nonzeros(LocB),1,[size(B,1),1]);
C=C(cB);
which returns
C =
2 1 0 2
or some crazy coding which seems to be faster for most instances:
[u,v,w]=unique([B;A],'rows');
wB=w(1:size(B,1));
wA=w(size(B,1)+1:end);
C=accumarray(wA,1,[numel(v),1]);
C=C(wB);