matlab: interpolation vector with a lot of duplicate values - matlab

Maybe my question might seem a little strange.
I have 3 vectors x1, y1, and x2
x1 and x2 have a lot of duplicate values, I would like to get y2 by interpolation with the same lenght of y1
y1 = [350 770 800 920 970 990 1020 1054 1080 1100];
x1=[10 10 11 14 13 12 10 10 10 7];
x2 = [10 10 13 13 15 13 13 10 10 10];
(actually have greater length, but always the same for all)
Is it impossible or a non-sense question? (in any case, my problem would remain unsolved)

Related

Reshaping a vector into a larger matrix with arbitrary m and n

I'm attempting to create a function that takes a vector of any length and uses its entries to generate a matrix of size mxn, where m and n are arbitrary numbers. If the matrix has a greater number of entries that the original vector, the entries should repeat. E.g. A vector, (1,2,3,4) would make a 3x3 matrix (1,2,3;4,1,2;3,4,1).
So far I have this function:
function A = MyMatrix(Vector,m,n)
A = reshape([Vector,Vector(1:(m*n)-length(Vector))],[m,n]);
end
which is successful in some cases:
>> m=8;n=5;Vector=(1:20);
>> A = MyMatrix(Vector,m,n)
A =
1 9 17 5 13
2 10 18 6 14
3 11 19 7 15
4 12 20 8 16
5 13 1 9 17
6 14 2 10 18
7 15 3 11 19
8 16 4 12 20
However this only works for values of m and n that multiply to a number less than or equal to twice the number of entries in 'Vector', so 40 in this case. When mn is larger than 40, this code yields:
>> m=8;n=6;Vector=(1:20);
>> A = MyMatrix(Vector,m,n)
Index exceeds the number of array elements (20).
Error in MyMatrix (line 3)
A = reshape([Vector,Vector(1:(m*n)-length(Vector))],[m,n]);
I have tried to create a workaround using functions such as repmat, however, so far I have not been able to create a matrix with larger m and n.
You only need to
index the vector using "modular", 1-based indexing;
reshape it taking into account that Matlab is column-major, so you need to swap m and n;
transpose to swap m and n back.
V = [10 20 30 40 50 60]; % vector
m = 4; % number of rows
n = 5; % number of columns
A = reshape(V(mod(0:m*n-1, numel(V))+1), n, m).';
This gives
A =
10 20 30 40 50
60 10 20 30 40
50 60 10 20 30
40 50 60 10 20

matlab : vectorization and fitlm

I have a vectorization problem with nlinfit.
Let A = (n,p) the matrix of observations and t(1,p) the explanatory variable.
For ex
t=[0 1 2 3 4 5 6 7]
and
A=[3.12E-04 7.73E-04 3.58E-04 5.05E-04 4.02E-04 5.20E-04 1.84E-04 3.70E-04
3.38E-04 3.34E-04 3.28E-04 4.98E-04 5.19E-04 5.05E-04 1.97E-04 2.88E-04
1.09E-04 3.64E-04 1.82E-04 2.91E-04 1.82E-04 3.62E-04 4.65E-04 3.89E-04
2.70E-04 3.37E-04 2.03E-04 1.70E-04 1.37E-04 2.08E-04 1.05E-04 2.45E-04
3.70E-04 3.34E-04 2.63E-04 3.21E-04 2.52E-04 2.81E-04 6.25E+09 2.51E-04
3.11E-04 3.68E-04 3.65E-04 2.71E-04 2.69E-04 1.49E-04 2.97E-04 4.70E-04
5.48E-04 4.12E-04 5.55E-04 5.94E-04 6.10E-04 5.44E-04 5.67E-04 4.53E-04
....
]
I want to estimate a linear model for each row of A without looping and avoid the loop
for i=1:7
ml[i]=fitlm(A(i,:),t);
end
Thanks for your help !
Luc
I believe that your probem is about undertanding how fitlm works, for matrix:
Let's work with the hald example for matlab:
>> load hald
>> Description
Description =
== Portland Cement Data ==
Multiple regression data
ingredients (%):
column1: 3CaO.Al2O3 (tricalcium aluminate)
column2: 3CaO.SiO2 (tricalcium silicate)
column3: 4CaO.Al2O3.Fe2O3 (tetracalcium aluminoferrite)
column4: 2CaO.SiO2 (beta-dicalcium silicate)
heat (cal/gm):
heat of hardening after 180 days
Source:
Woods,H., H. Steinour, H. Starke,
"Effect of Composition of Portland Cement on Heat Evolved
during Hardening," Industrial and Engineering Chemistry,
v.24 no.11 (1932), pp.1207-1214.
Reference:
Hald,A., Statistical Theory with Engineering Applications,
Wiley, 1960.
>> ingredients
ingredients =
7 26 6 60
1 29 15 52
11 56 8 20
11 31 8 47
7 52 6 33
11 55 9 22
3 71 17 6
1 31 22 44
2 54 18 22
21 47 4 26
1 40 23 34
11 66 9 12
10 68 8 12
>> heat
heat =
78.5000
74.3000
104.3000
87.6000
95.9000
109.2000
102.7000
72.5000
93.1000
115.9000
83.8000
113.3000
109.4000
This means that you have a matrix ingredients column % of ingredients in a component
>> sum(ingredients(1,:))
ans =
99 % so it is near 100%
and the rows are the 13 measures of the prodcut and the heat vector, the heat at the observation was taken.
>> mdl = fitlm(ingredients,heat)
mdl =
Linear regression model:
y ~ 1 + x1 + x2 + x3 + x4
Estimated Coefficients:
Estimate SE tStat pValue
________ _______ ________ ________
(Intercept) 62.405 70.071 0.8906 0.39913
x1 1.5511 0.74477 2.0827 0.070822
x2 0.51017 0.72379 0.70486 0.5009
x3 0.10191 0.75471 0.13503 0.89592
x4 -0.14406 0.70905 -0.20317 0.84407
Number of observations: 13, Error degrees of freedom: 8
Root Mean Squared Error: 2.45
R-squared: 0.982, Adjusted R-Squared 0.974
F-statistic vs. constant model: 111, p-value = 4.76e-07
So in your case, it not have sense to measure for each observation separately. is simply with t the same number of elements than observations.
take a look here
mdl = fitllm(A,t)
Problem solved using sapply and findgroups !

MATLAB - Error bars separation distance and height

I have data arrays w and x; I want to plot error bars y distance apart and z distance above and below the points. Is there a way to do this? I've tried manipulating the errorbar function but can't figure it out.
w [1
3
5
8
9
15
17
34
67
79
90
123
63
23
2
]
x[1
2
3
4
5
6
7
8
9
10
11
12
13
14
15]
plot(x,w)hold on;
errorbar(x,w....not sure what to put after);
I'm trying to plot error bars every 3rd point and with a height of +-5
You could simply draw the error bars yourself
for idx = 1:3:length(w)
plot([x(idx) x(idx)],[w(idx)+5 w(idx)-5]);
end
Alternatively you could give a handle to the errorbar function, but I'm not sure if it allows you to modify this stuff.
By setting the right Properties of the errorbarobject you can get what you need.
Note the LData and UData properties, which are used to specify the height below and above the bars as well as the XData and YData.
clear
clc
close all
w = [1 3 5 8 9 15 17 34 67 79 90 123 63 23 2 ];
x = [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15];
%// Set location on x axis
loc = 1:3:numel(w);
plot(x,w)
hold on;
hErr = errorbar(loc,w(loc),'rx','LData',5,'UData',5,'XData',loc,'YData',w(loc));
Output:

how to multiply 2D slices of two 3D matrices with each other in Matlab

I have two 3D matrices A(kl,1,r) and B(1,rs,r). kl=rs.
I need to get a new matrix C(kl,rs,r) which should have the product of column vector of A(kl,1) by the row vector of B(1,rs) for every page r without for loop
C=zeros(size(A,1),size(B,2),r);
for rr=1:size(A,3)
dummy=squeeze(A(:,:,rr))*squeeze(B(:,:,rr))';
C(:,:,rr)=dummy;
end
can anyone help with that? :)
Using bsxfun, you could do that directly in one line
out = bsxfun(#times, A, B);
Sample Inputs:
>> A
A(:,:,1) =
6
10
3
A(:,:,2) =
2
2
1
>> B
B(:,:,1) =
5 5 4
B(:,:,2) =
8 7 8
Results:
out(:,:,1) =
30 30 24
50 50 40
15 15 12
out(:,:,2) =
16 14 16
16 14 16
8 7 8

Extracting values along a row, column or diagonal of a 2D matrix

I have a 2 dimensional matrix and I want to get the data along a particular line. Similar to what 'Slice' does to a 3D matrix. Is there a a way to do a similar thing on a 2D matrix.
Thanks in advance.
Extracting all values of a column or a line:
>> M = magic(4)
M =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
>> particular_row = 3;
>> M(particular_row,:)
ans =
9 7 6 12
>> particular_column = 2;
>> M(:,particular_column)
ans =
2
11
7
14
Extracting values along a diagonal:
What if I want to get the data along any direction say along a line joining matrix index (1,1) to (4,4) of a 5x5 matrix?
I'd use linear indexing and the sub2ind function for this task. Demo:
(1,1) to (4,4):
>> M = magic(5)
M =
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
>> M(sub2ind(size(M), 1:4, 1:4))
ans =
17 5 13 21
Another example: (1,2) to (3,4):
M(sub2ind(size(M), 1:3, 2:4))
ans =
24 7 20