I have two vectors (latg and ch4): latg is a 18225x1 vector, and ch4 is a 91269x1 vector. I want to size the latg vector so that it matches the size of the ch4 vector by interpolating between the data points.
How do I go about this? Do I use the interp1 function?
Yes indeed the interp1 function can be used. I assume you want to linearly interpolate extra datapoints between the limiting values of the smaller array.
% Vector to be upscaled
vector1 = latg;
% Number of elements in new vector (vector2, nr of elements same as in ch4)
n_vector2 = length(ch4);
vector2 = interp1( linspace(0,1,length(vector1)), vector1, linspace(0,1,n_vector2));
So now vector2 is the same of vector latg except that it contains the same amount of points as vector ch4 obtained by linear interpolation.
Kind regards,
Ernst Jan
Related
I am converting numpy code to matlab. tensor is a 3D matrix of 6 x 2D matrices of the tensor components. This code appears to then split them back into those 6 separate 2D matrices.
gxx, gxy, gxz, gyy, gyz, gzz = tensor
Can I do this as eloquently in matlab?
re OmG: gxx, etc are the six tensor components of a gravity grid. xx for 2nd derivative of x in the x direction, xy is the 2nd derivative of x in the y direction, etc. Those components will be put through a simple equation to calculate the invariants which will then calculate the depth of the gravity anomaly.
As #Div-iL says, you could simply assign each variable to a slice of the 3D array:
tensor = rand(5,3,6); % Random data to play with
gxx = tensor(:,:,1);
gxy = tensor(:,:,2);
% etc
However if you really wanted to do it automatically you could generate a cell-array of 2D arrays (using mat2cell) and then assign them to variables using a comma-separated list assignment:
[nx,ny,nz] = size(tensor);
ca = mat2cell(tensor, nx, ny, ones(1,nz));
[gxx, gxy, gxz, gyy, gyz, gzz] = ca{:};
However, that all feels a bit hairy to me. If you're looking for a natively-supported one-liner (like your example) then I think you're out of luck.
I have results of an experiment which is parameter optimization of support vector regression. I have the first three columns the parameters and the last column the MSE so I have about 24,646 X 4 matrix. I would like to get the parameters corresponding to the smallest MSE. How can I plot this on Matlab? the elements of the matrix are float numbers. Thank you for your help.
Use second output of min to obtain the index of the minimum value in the fourth columns; and then use that to get the parameters:
[~, ind] = min(matrix(:,4));
params = matrix(ind,1:3);
I have a 512x512x3 matrix that stores 512x512 there-dimensional vectors. What is the best way to normalize all those vectors, so that my result are 512x512 vectors with length that equals 1?
At the moment I use for loops, but I don't think that is the best way in MATLAB.
If the vectors are Euclidean, the length of each is the square root of the sum of the squares of its coordinates. To normalize each vector individually so that it has unit length, you need to divide its coordinates by its norm. For that purpose you can use bsxfun:
norm_A = sqrt(sum(A .^ 2, 3)_; %// Calculate Euclidean length
norm_A(norm_A < eps) == 1; %// Avoid division by zero
B = bsxfun(#rdivide, A, norm_A); %// Normalize
where A is your original 3-D vector matrix.
EDIT: Following Shai's comment, added a fix to avoid possible division by zero for null vectors.
Imagine a set of data with given x-values (as a column vector) and several y-values combined in a matrix (row vector of column vectors). Some of the values in the matrix are not available:
%% Create the test data
N = 1e2; % Number of x-values
x = 2*sort(rand(N, 1))-1;
Y = [x.^2, x.^3, x.^4, x.^5, x.^6]; % Example values
Y(50:80, 4) = NaN(31, 1); % Some values are not avaiable
Now i have a column vector of new x-values for interpolation.
K = 1e2; % Number of interplolation values
x_i = rand(K, 1);
My goal is to find a fast way to interpolate all y-values for the given x_i values. If there are NaN values in the y-values, I want to use the y-value which is before the missing data. In the example case this would be the data in Y(49, :).
If I use interp1, I get NaN-values and the execution is slow for large x and x_i:
starttime = cputime;
Y_i1 = interp1(x, Y, x_i);
executiontime1 = cputime - starttime
An alternative is interp1q, which is about two times faster.
What is a very fast way which allows my modifications?
Possible ideas:
Do postprocessing of Y_i1 to eliminate NaN-values.
Use a combination of a loop and the find-command to always use the neighbour without interpolation.
Using interp1 with spline interpolation (spline) ignores NaN's.
I have found a bit of code in MATLAB:
y = fft(y, nfft);
Where y is a 512x443 two-dimensional array and nfft = 512.
I thought that fft is for a one-dimensional array and for a two-dimensional array there should be fft2, but fft is working. How is that possible?
From the documentation (emphasis mine):
Y = fft(x) returns the discrete Fourier transform (DFT) of vector x, computed with a fast Fourier transform (FFT) algorithm.
If the input X is a matrix, Y = fft(X) returns the Fourier transform of each column of the matrix.
fft actually takes an (optional) third argument: dim. This tells it which dimension to perform the FFT on.
If you don't specify it, dim will default to the "first nonsingleton dimension". So you're getting a one-dimensional FFT of all the columns of your two-dimensional array.