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
Related
I want to repeat a row vector to create a matrix, in which every row is a slightly modified version of the original vector.
For example, if I have a vector v = [10 20 30 40 50], I want every row of my matrix to be that vector, but with a random number added to every element of the vector to add some fluctuations.
My matrix should look like this:
M = [10+a 20+a 30+a 40+a 50+a;
10+b 20+b 30+b 40+b 50+b;
... ]
Where a, b, ... are random numbers between 0 and 2, for an arbitrary number of matrix rows.
Any ideas?
In Matlab, you can add a column vector to a matrix. This will add the vector elements to each of the row values accordingly.
Example:
>> M = [1 2 3; 4 5 6; 7 8 9];
>> v = [1; 2; 3];
>> v + M
ans =
2 3 4
6 7 8
10 11 12
Note that in your case v is a row vector, so you should transpose it first (using v.').
As Sardar Usama and Wolfie note, this method of adding is only possible since MATLAB version R2016b, for earlier versions you will need to use bsxfun:
>> % instead of `v + M`
>> bsxfun(#plus, v, M)
ans =
2 4 6
5 7 9
8 10 12
If you have a MATLAB version earlier than 2016b (when implicit expansion was introduced, as demonstrated in Daan's answer) then you should use bsxfun.
v = [10 20 30 40 50]; % initial row vector
offsets = rand(3,1); % random values, add one per row (this should be a column vector)
output = bsxfun(#plus,offsets,v);
Result:
>> output =
10.643 20.643 30.643 40.643 50.643
10.704 20.704 30.704 40.704 50.704
10.393 20.393 30.393 40.393 50.393
This can be more easily understood with less random inputs!
v = [10 20 30 40 50];
offsets = [1; 2; 3];
output = bsxfun(#plus,offsets,v);
>> output =
11 21 31 41 51
12 22 32 42 52
13 23 33 43 53
Side note: to get an nx1 vector of random numbers between 0 and 2, use
offsets = rand(n,1)*2
I have a matrix M[1,98] and a matrix N[1,x], let's assume in this case x =16.
What I want is to multiply N by M , make the sum by element, and increment the matrix M. With the finality of getting an output of [1,98].
It's a bit confusing. An example:
M=[2 3 4 5 6 7]
N=[1 2 3]
it1=(2*1)+(3*2)+(4*3)+(5*0)+...=20
it2=(3*1)+(4*2)+(5*3)+(6*0)+...=26
it3=..
Output=[20 26 ... ... ... ...]
Like that until the end but considering the size of the matrix N variable. M has always the same size.
That's a convolution:
result = conv(M, N(end:-1:1), 'valid');
To achieve the result you want you need to flip the second vector and keep only the "valid" part of the convolution (no border effects).
In your example:
>> M = [2 3 4 5 6 7];
>> N = [1 2 3];
>> result = conv(M, N(end:-1:1), 'valid')
result =
20 26 32 38
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])
I have two vectors in MATLAB, say:
x = [1 20 3 7 10]
and
y = [2 51 1 9 18]
How can I plot y vs K where x has sorted value order (1 3 7 10 20) with their respective y values like the following?
x = [1 3 7 10 20]
y = [2 1 9 18 51]
Call sort with a second output argument.
x = [1 20 3 7 10]
y = [2 51 1 9 18]
[xsorted, I] = sort(x)
ysorted = y(I)
XY = sortrows([x ; y]');
plot(XY(:,1), XY(:,2));
Concatenate the matrices, transpose them and then you can use sortrows to order by X
I have two matlab vectors. The first has N elements, the other has k*N. I know what k is, and I want to splice the lists such that each element from the first vector appears before the corresponding k elements from the next vector. For example:
k = 3
x = [1 5 9]
y = [2 3 4 6 7 8 10 11 12]
should be combined to look like this:
z = [1 2 3 4 5 6 7 8 9 10 11 12]
Is there an easy way to do this quickly? My x's and y's are pretty big. Thanks!
You can do this via some reshaping
k = 3
x = [1 5 9]
y = [2 3 4 6 7 8 10 11 12]
%# make a k-by-n array
z = reshape(y,k,[]);
%# catenate with x
z = [x;z];
%# reorder
z = z(:)'