How to solve error while doing interpolation one point with many points in MATLAB - matlab

I am trying to interpolate many points to one point in MATLAB. But I am getting error
The grid vectors do not define a grid of points that match the given values.
My code is given below
A = [2 6;3 7]
B = [3 6;4 9]
Xq=[A(:,1) [10;10]]
Yq=interp1(A,B,Xq','linear','extrap')
Actually I want to generate a line passing through one point from many points and as shown in below figure and extend upto axis of plot

'For loop' would be a solution.
One example is this.
A = [2 6;1 6;3 6]
B = [3 6;2 6;3 6]
L=size(A)
xArr=zeros(L(1), L(2));
yArr=zeros(L(1), L(2));
nLine=L(1); %number of lines
for k=1:nLine
Xq=[A(k,:) 10]
Yq=interp1(A(k,:),B(k,:),Xq','linear','extrap')
xArr(:,k)=Xq';
yArr(:,k)=Yq';
end
plot(xArr,yArr,'*-')
If you don't want to use for loop, you could directly calculate gradient and intercept of linear function.
A = [2 6;1 6;3 6]
B = [3 6;2 6;3 6]
L=size(A)
limX=10; %x limit
a=(B(:,2)-B(:,1))./(A(:,2)-A(:,1)); %gradient vector
b=B(:,1)-a.*A(:,1); % intercept vector
y=a*limX+b % linear function
Aq=[A';limX*ones(1,L(1))];
Bq=[B';y'];
figure(2)
plot(Aq,Bq,'*-')

Related

convert a line plot to a matrix

I connect several points two by two by
plot([x1,x2],[y1,y2]) %create line
% x1,y1 = coordinates of point 1
% x2,y2 = coordinates of point 2
and I would like to know if it's possible to keep this different links into a matrix to display them subsequently with imagesc or imshow (this matrix will also be useful to me outside the display later)
As long as I understand, you are trying to plot multiple "lines" in one plot without using hold.
You can use concatenation for this purpose as shown below:
plot([1 ;2 ],[1;2] );
xlim([1 6]);
ylim([1 6]);
plot([3 ;4 ],[3;4] )
xlim([1 6]);
ylim([1 6]);
plot([5 ;6 ],[5;6] );
xlim([1 6]);
ylim([1 6]);
Finally if you want to plot these all lines into one plot you can use concatenation in the second dimension.
plot([1 3 5 ; 2 4 6 ],[1 3 5 ; 2 4 6] );
In order to save it and make it again plot table you can store in matrix with another dimension
matrixTest(:,:,1) = [1 3 5 ; 2 4 6 ];
matrixTest(:,:,2) = [1 3 5 ; 2 4 6 ];
plot(matrixTest(:,:,1),matrixTest(:,:,2));

Matrix access by x/y coordinates without linear indexing and looping in Matlab/Octave

I'm operating on very big, 2D, sparse matrices in Octave. I have hit the linear indexing limit of 2^31 and need to go bigger.
The problem is I have two same-sized vectors of X and Y coordinates and would like to modify respective points without a loop.
I have already tried looping over one dimension, and arrayfun - both work, but suffer from serious perfomance issues.
Is there any way to do this without recompiling Octave for 64bit linear indexing?
Example of what I would like to achive:
A = [1 2 3; 4 5 6; 7 8 9];
x = [1 3]; y = [3 2];
B = getxy/setxy(A, x ,y) % [7 6]

Matlab: Solve Exponential Equation with two unknown parameters

I have a Matrix called A. For example the following:
A = [1 2 3; 3 4 1; 2 4 4]
Now I have the following equation:
A(x,y) = (j^x)*(i^y)
j and i are normal values (dimension 1x1), not indices of a matrix. ^
Lets make an example:
A(1,1) = 1 (First value of the Matrix)
1 = (j^1)*(i^1)
And a second one:
A(1,2) = 3
3 = (j^1)*(i^2)
Is there a possibility to receive one solution for the two parameters using Matlab?
Here is some code that can find the best solution to your problem, if there is one. In this case, there is no reasonable solution, but defining A by M([4 2]) (for example) does work reasonably well.
A = [1 2 3; 3 4 1; 2 4 4] %// the A matrix
[C,R]=meshgrid(1:3) %// create matrices of row/column indices
M=#(xy) xy(2).^C.*xy(1).^R %// calculates matrix of elements j^x*i^y
d=#(xy) A-M(xy) %// calculates difference between A and the calculated i^x*y^j matrix
r=fsolve(#(xy) norm(d(xy)),[1 1]) %// use fsolve to attempt to find a solution
d(r) %// show resulting difference between target matrix and solution matrix
norm(d(r)) %// norm of that matrix
M(r) %// show the solution matrix

Plotting same elements of two row vectors of different sizes

Let us have
a=[1 2 3 4 5];
b=[4 2];
I want a plot of 'a' in which same elements(of a and b) get marked. I am trying but all in vain. My result should be plot of a with values 4 and 2 marked.
Please help.
a = [1 2 3 4 5];
x = 1:numel(a);
b = [2 4];
figure
plot(x, a);
hold on;
markIt = ismember(a, b);
plot(x(markIt), a(markIt), 's')
This produces a plot of a, with the elements that also belong to b (found with the ismember function) plotted (again) as a square (that's the 's' in the second plot command).
I am sure you can adapt this to your needs... Sorry I can't test while I'm at home.
I'm unclear as to how you are going to plot a, but you can find the elemnts of a that are also in b using
a(any(bsxfun(#eq,a,b')))

Removing the line between two specific data points in Matlab

I am going to draw a graph in Matlab. The graph is quite simple and I am using the plot function.
Suppose the data that I want to plot is (0:1:10). I also put markers on my graph. Then, we have a line that has markers on coordinates (0,0),(1,1),(2,2),... etc.
Now, I want to remove the line between (2,2) and (3,3) without deleting the whole line. That is to say, my purpose is to get rid of a particular segment of the line without loosing the entire line or any marker points.
How can I do that?
Removing the section of line after you have plotted it is difficult. You can see that the line is made up of one MATLAB object by the following code:
x = 1:10;
y = 1:10;
H = plot(x, y, '-o');
get(H, 'children')
ans =
Empty matrix: 0-by-1
We can see that the line has no children, so there are no 'subparts' that we can remove. However, there are some cheeky tricks we can use to try to achieve the same effect.
Plot two lines separately
...using hold on. See Victor Hugo's answer. This is the proper way of achieving our goal.
Plot two separate lines in one
MATLAB doesn'y plot points with a NaN value. By modifying the input vectors, you can make MATLAB skip a point to give the effect of a broken line:
x = [0 1 2 2 3 4 5 6 7 8 9];
y = [0 1 2 nan 3 4 5 6 7 8 9];
plot(x, y, '-o');
This is equivalent to plotting a line from [0, 0] to [2, 2], skipping the next point, then starting again at [3, 3] and continuing to [9, 9].
'Erase' part of the line
This is the nastiest way of doing it, but is a cheap hack that could work if you can't be bothered with changing your input arrays. First plot the line:
x = 1:10; y = 1:10;
plot(x, y, '-o');
Now plot a white line over the part you wish to erase:
hold on
plot([2 3], [2 3], 'w');
As you can see, the result doesn't quite look right, and will respond badly if you try to do other things to the graph. In short, I wouldn't recommend this method but it might come in useful in desperate times!
Try the following:
y = [0.2751 0.2494 0.1480 0.2419 0.2385 0.1295 0.2346 0.1661 0.1111];
x = 1:numel(y);
plot(x(1:4), y(1:4), '-x')
hold
plot(x(5:end), y(5:end), '-x')