Matlab - Merge two vectors and a matrix with different dimensions - matlab

I have two vectors and matrix, for example:
a = [ 1 2 3 4];
b = [6 7 8];
c = [ 600 700 800 900;
100 200 300 400;
777 888 555 333];
I would like to get a matrix as:
1 6 600
2 6 700
3 6 800
4 6 900
1 7 100
2 7 200
3 7 300
4 7 400
1 8 777
2 8 888
3 8 555
4 8 333
Is it possible to get this matrix without using loops?

Sure, with meshgrid for instance:
[B, A] = meshgrid(b, a);
C = c';
Res = [A(:) B(:) C(:)];
Best,

Related

log sacle colorbar: how to set ticks?

I want to set the ticks of the colorbar to be in log scale, with simple readable ticks: 2 3 4 5 6 7 8 9 10 20 30 (and not just "10^0", "10^1");
for example I do:
x = linspace(0,0.9);
y=logspace(-1,1);
[X,Y] = meshgrid(x,y);
Z = 220 *sqrt((1-X).*Y); %just random function(x,y)
[M,c]= contourf(X,Y,Z,100);
c.LineColor = 'none';
set(gca,'ColorScale','log')
cl=colorbar;
ylabel(cl, 'color')
cl.Ticks=[ 2 3 4 5 6 7 8 9 10 20 30];
cl.TickLabels = num2cell([ 2 3 4 5 6 7 8 9 10 20 30]);
It doesn't work that way. How to do it?
Your code works well and as expected. If you check your colorbar, the lower limit value is 22, and therefore you only see the last label.
x = linspace(0,0.9);
y=logspace(-1,1);
[X,Y] = meshgrid(x,y);
Z = 220 *sqrt((1-X).*Y); %just random function(x,y)
[M,c]= contourf(X,Y,Z,100);
c.LineColor = 'none';
set(gca,'ColorScale','log')
cl=colorbar;
ylabel(cl, 'color')
cl.Limits=[2 cl.Limits(2)] % change the range
cl.Ticks=[ 2 3 4 5 6 7 8 9 10 20 30];
cl.TickLabels = num2cell([ 2 3 4 5 6 7 8 9 10 20 30]);

Reshaping 3 dimensional array to 2 by stacking matrixes horizontally [duplicate]

This question already has an answer here:
Reshape matrix from 3d to 2d keeping specific order
(1 answer)
Closed 5 years ago.
I have 4x3x2 array:
A(:,:,1) =
1 10 100
2 20 200
3 30 300
4 40 400
A(:,:,2) =
5 50 500
6 60 600
7 70 700
8 80 800
I want to reshape it to B matrix with size 8x3 preserving the structure of each matrix as:
B =
1 10 100
2 20 200
3 30 300
4 40 400
5 50 500
6 60 600
7 70 700
8 80 800
Any idea how to do it in a simple and neat way?
As seen here.
Method 1: permute and reshape
B = reshape(permute(A, [2 1 3]), size(A, 2), [])'
Method 2: cell -> matrix
B = num2cell(A, [1 2]);
B = vertcat(B{:})

Matlab - Finding values within a matrix

How can I find the row that have all the values from A into the matrix B and display the index of the rows using Matlab?
A= [2 5 6];
B=[1 2 4 9 10 15 27 30;
1 2 3 4 5 6 7 8;
1 2 3 5 6 9 22 101;
2 4 5 6 14 20 22 23]
Thanks
With bsxfun in 3D -
ind = find(all(any(bsxfun(#eq,B,permute(A,[1 3 2])),2),3))
With bsxfun again, but keeping it in 2D -
ind = find(sum(reshape(any(bsxfun(#eq,B(:),A(:).'),2),size(B)),2)==numel(A))
With ismember -
ind = find(sum(reshape(ismember(B(:),A(:)),size(B)),2)==numel(A))
With pdist2 from Statistics and Machine Learning Toolbox -
ind = find(sum(reshape(any(pdist2(B(:),A(:))==0,2),size(B)),2)==numel(A))
With knnsearch again from Statistics and Machine Learning Toolbox-
[~,dists] = knnsearch(A(:),B(:))
ind = find(sum(reshape(dists==0,size(B)),2)==numel(A))
Sample run -
A =
2 5 6
B =
1 2 4 9 10 15 27 30
1 2 3 4 5 6 7 8
1 2 3 5 6 9 22 101
2 4 5 6 14 20 22 23
ind =
2
3
4

Multiply each value in rows of Matrix A by each corresponding value of a specfic row in Matrix B

I have a A=[m,n] matrix and a B=[n,l] matrix.
A =
[1 2 3
4 5 6
7 8 9
10 11 12]
For the sake of simplicity, let's assume l=1, so B is in fact a vector B=[n,1]
B = [100 10 1]
I would like multiply all the values in each row of A by a corresponding value of B - column-wise.
I know how to do it "manually":
C=[A(:,1)*B(:,1), A(:,2)*B(:,2), A(:,3)*B(:,3)]
This is the result I want:
C = [100 20 3
400 50 6
700 80 9
1000 110 12]
Unfortunately my real life matrices are a bit bigger e.g. (D=[888,1270]) so I'm looking for smarter/faster way to do this.
Pre R2016b:
C=bsxfun(#times,A,B)
C =
100 20 3
400 50 6
700 80 9
1000 110 12
R2016b and later:
In MATLABĀ® R2016b and later, you can directly use operators instead of bsxfun , since the operators independently support implicit expansion of arrays.
C = A .* B
If I > 1, then you will have to reorder the dimensions of B first with a permute,
>> B = [100 10 1; 1 10 100];
>> C = bsxfun(#times, A, permute(B, [3 2 1]));
>> C
C(:,:,1) =
100 20 3
400 50 6
700 80 9
1000 110 12
C(:,:,2) =
1 20 300
4 50 600
7 80 900
10 110 1200

How to sum values among matrices of structures under certain conditions in Matlab?

I am new in matlab and I am not familiar with array of matrices. I have a number of matrices nx6:
<26x6 double>
<21x6 double>
<27x6 double>
<36x6 double>
<29x6 double>
<30x6 double>
....
Each matrix is of this type:
>> Matrix{1,1}
A B C D E F
1 2 6 223 735064.287500000 F11
2 3 6 223 735064.288194445 F12
3 4 6 223 735064.288888889 F13
4 5 6 223 735064.290277778 F14
>> Matrix{2,1}
A B C D E F
1 2 6 223 735064.700694445 F21
2 3 6 223 735064.701388889 F22
3 4 6 223 735064.702083333 F23
4 5 6 223 735064.702777778 F24
>> Matrix{3,1}
A B C D E F
1 2 7 86 735064.3541666666 F31
2 3 7 86 735064.3548611112 F32
3 4 7 86 735064.3555555555 F33
4 5 7 86 735064.3562499999 F34
5 6 7 86 735064.702777778 F35
>> Matrix{4,1}
A B C D E F
1 2 7 86 735064.3569444444 F41
2 3 7 86 735064.3576388888 F42
3 4 7 86 735064.3583333333 F43
4 5 7 86 735064.3590277778 F44
5 6 6 86 735064.702777778 F45
Where E and F are dates in datenum format. Specifically F is the time difference.
Considering all matrices at once, I would like to sum the values of column F among all the matrices that have equal values in columns A, B, D.
For each value of the column D (the number of bus), I would like to obtain a new matrix like the following one:
A B C D H
1 2 6 223 F11+F21
2 3 6 223 F12+F22
3 4 6 223 F13+F23
4 5 6 223 F14+F24
A B C D H
1 2 7 86 F31+F41
2 3 7 86 F32+F42
3 4 7 86 F33+F43
4 5 7 86 F34+F44
5 6 7 86 F35+F45
Thank you in advance for you help!
This approach should get you started. I suggested setting up a matrix that stores the comparison between the columns 1,2 and 4. Based on that matrix you can then generate your output matrix. This saves you nested if statements and checks in your loop.
Here's an example (please note that I changed row 3 of Matrix{1,1}):
Matrix{1,1} = [ ...
1 2 6 223 735064.287500000 1;
2 3 6 223 735064.288194445 2;
3 4 6 223 735064.288888889 3;
4 5 6 223 735064.290277778 4];
Matrix{2,1} = [ ...
1 2 6 223 735064.700694445 10;
2 3 6 223 735064.701388889 10;
2 4 6 223 735064.702083333 10;
4 5 6 223 735064.702777778 10];
COMP = Matrix{1,1}(:,[1:2 4])==Matrix{2,1}(:,[1:2 4]);
a = 1;
for i=1:size(Matrix{1,1},1)
if sum(COMP(i,:)) == 3
SUM{1,1}(a,1:5) = Matrix{1,1}(i,1:5);
SUM{1,1}(a,6) = Matrix{1,1}(i,6) + Matrix{2,1}(i,6);
a = a + 1;
end
end
The matrix COMP stores a 1 for each element that is the same in Matrix{1,1} and Matrix{2,1} when comparing columns 1, 2 and 4.
This reduces the if-statement to a check if all elements in a row agree (hence sum == 3). If that condition is satisfied, a new matrix is generated (SUM{1,1}) which sums the entries in column 6, in this case:
SUM{1,1}(:,6) =
11
12
14