Extend coordinate vector [duplicate] - matlab

This question already has answers here:
How do I double the size of a vector in MATLAB with interpolation?
(2 answers)
Closed 8 years ago.
I have a row vector like
x = [ 0.125 0.25 0.5 0.75 1];
And I would like to expand it to 100 points with interpolation between points. Ho can i do it so that at the end I have equally spaced points but a length of 100 points?
Thanks

Solution
xi = [0 25 50 75 100];
yi = [0.125 0.25 0.5 0.75 1];
x = 1:1:100;
y = interp1(xi, yi, x);
Solution is y.
Explanation:
I consider your vector [0.125 0.25 0.5 0.75 1] as the result of a function F such that F(xi) = yi where xi = [0 25 50 75 100] and yi = [0.125 0.25 0.5 0.75 1].
I create x a 100-size vector using the same interval as xi;
I compute the interpolation of x based on the relationship between xi and yi;

Related

How to create a contourf plot from a table?

As far as I understand the way to 3-d plot/ surface plot is "meshgrid".
But the data I have has a specific format:
X
Y
Z
1
0.1
10
1
0.2
12
1
0.3
13
2
0.1
11
2
0.2
12
2
0.3
14
3
0.1
11
3
0.2
12
3
0.3
15
The first and second column (X and Y) repeat themselves in that fashion, and I need to plot Z(X,Y).
How Do I do it?
X = 1:1:3 % grid can be set by beginning:step:end
Y = 0.1:0.1:0.3
[x,y] = meshgrid(X,Y); % then make 2d domain
% z values have to be done manually or can automate if you read your data from txt file
z = [10 12 13; 11 12 14; 11 12 15];
% and finally
surf(x,y,z)

Brute force in Matlab

this is my problem, for example i have an equation x + y =2, so using matlab i want to know how to determine all the possible combination of values of x and y when you add it, and will give sum of 2 (ex: x1 = 0.98, y1 =0.12; x2=0.94 y2=0.16, and etc)
i think i need to use for loop?
for x = 2-y
end
for y =2-x
end
Values of x and y
x y
0 2
0.1 1.9
0.2 1.8
0.3 1.7
0.4 1.6
0.5 1.5
0.6 1.4
0.7 1.3
0.8 1.2
0.9 1.1
1 1
so guys i need your help thanks
To get all possible combinations of x and y between 0 and 2 with a step size of 0.1 you don't even need a for loop. You can create a vector x which contains all possible x values and then calculate the corresponding y's:
x = 0:0.1:2; % Create a vector of values between 0 and 2 in steps 0f 0.1
y = 2 - x;
This will give you two (row) vectors containing all possible combinations which add up to 2.

contour plot with non integer coordinates

I have a matrix containing X coordinates, Y coordinates and V (value to be plotted).
X and Y values can be non integer.
I want to plot V values with a contour plot, but this type of plot only accepts a matrix as input.
My idea is to change X and Y non-integers coordinates into integers so V can be plotted with contour function.
Any idea how can I change the X and Y values to integers? (I don't care about the X an Y coordinates in this case)
Here's an example of a matrix:
H=[-0.5 0 20; 0 0 15 ; 0.5 0 40; -0.5 0.5 18; 0 0.5 35; 0.5 0.5 10; -0.5 1 3; 0 1 9; 0.5 1 20]
Just arrange your values to a matrix.
do something like:
[xcoord,ix] = unique(H(:,1));
[ycoord,iy] = unique(H(:,2));
matrix = nan(numel(xcoord),numel(ycoord))
and then sort your values in, the ix and iy are your integers...

scale part of an axis in matlab

I have the following image which I want to have the depth axis range like below :
(10 9.5 9 8.5 8 7.5 7 6 5 3 2 1 0.9 0.8 0.7 0.6 0.5 0.4 0.3 0.2 0.1 0) to show the data between depth 1 and 0 in larger scale, and I have the following code
depths = [10 5 1 0.5 0; 10 5 1 0.5 0] % these are the real depths in meter
contourf(points,depths,RFU15102013_BloomAsMainPoint);
set(gca, 'XTick', points(1) : points(2), 'XTickLabel',{ 'LSB1', 'LSB2'});
ylabel('Depth(m)');
xlabel('Points');
title('Date: 15.10.2013');
this is the image :
how can I do that?
EDIT1
Real Data:
RFU15102013_BloomAsMainPoint = [ 2.71 1.23 1.30 1.20 14.37 ; 2.51 1.36 1.01 1.24 1.15];
points = [1 1 1 1 1; 2 2 2 2 2 ];
depths = [10 5 1 0.5 0; 10 5 1 0.5 0];
As most of a data changes around zero it could be enough to change scaling of Y axis. Here is an example
close all; clear all;
z = [ 2.71 1.23 1.30 1.20 14.37 ; 2.51 1.36 1.01 1.24 1.15];
x = repmat([1; 2], 1, 5);
y = repmat([10 5 1 0.5 0], 2, 1);
% plotting with equally spaced y-s
h = subplot(1,2,1);
contourf(x,y,z);
y2 = log(y + 0.25);
yTicks = linspace(min(y2(1,:)), max(y2(1,:)), 10);
% plotting with logarithmically spaced y-s
h = subplot(1,2,2)
contourf(x,y2,z);
set(h,'YTick', yTicks)
set(h,'YTickLabel', exp(yTicks) - 0.25);
print('-dpng','scaling.png')
The result
This way any monotonic continuous function for axis scaling can be applied.
You could use UIMAGE - UIMAGESC from the mathworks file exchange and set the y values to emphaisize points in 1 to 0 range.

How to calculate the slope in percentage of linearly fitted data in Matlab

I hope someone can help me.
Lets say I have the following two vectors
t = [1 2 3 4 5];
m = [10 8 6 4 2];
plot(t,m)
And I want to find the slope of the linear fit (1. degree)
so I write:
polyfit(t,m,1)
I then obtain the following answer:
ans =
-2.0000 12.0000
Meaning that y = -2x + 12
How do I re-calculate the coefficient to a percentage slope?
The reason I am interested in this is that I want to discard all data that has a slope < 80% (and proceed with data with slope coefficients between 80% and 100%).
Assuming that you define percentage slope by the formula given in #2 of the Nopmenclature section on the Wikipedia Grade page, 100 * dy / dx, your percentage slope is just the coefficient of x^1, multiplied by 100. You can do a test to check for slopes < 80% as follows:
t = [1 2 3 4 5];
m = [10 8 6 4 2];
p = polyfit(t,m,1);
g = p(1) * 100;
if g > 80 && g < 100
% Do what you need to do...
end