Rebuild the matrix according to the values of its elements

1 view (last 30 days)
xm=6; ym =3:0.01:10;
[y, x] = meshgrid(ym,xm);
angle1 = asind((-x.^2.*y.^2-y.^4+1296)./(2.*x.*y.^3));
angle1 = real(angle1);
z1 = 6./(x.*sind(angle1)+y);
figure
plot(z1,angle1);
I want to obtain z2 matrix which gives, z2(i) = z1(i) + 0.2, which means ith element of z2 is equal to ith element of z1 + 0.2. And also I need to obtain angle2 matrix which gives a matrix of angles corresponding to z2 matrix. However, elements of z2 matrix has so much fractions which makes finding an angle corresponding to z1+0.2 impossible. I want to a solution which can find and place the element, which is closest value to z(i)+0.2 with an error margin 0.05, to rebuild z2 matrix. If there is no element in this error margin give NaN. As a result of that, please find angle2 matrix corresponding to z2 matrix rebuilded. As a manual example;
z1= [1.153 1.451 1.746 1.349 1.255 1.959 1.550 1.854 1.652 ]
angle1= [10 25 35 20 45 30 15 50 55 ]
z2 = [ 1.349 1.652 1.959 1.550 1.451 NaN 1.746 NaN 1.854]
angle2 = [ 20 55 30 15 25 NaN 35 NaN 50]
  1 Comment
Bob Thompson
Bob Thompson on 18 Apr 2019
For the first part, you just need:
z2 = z1 + 0.2;
For the second part, angle2 is an array of angles, but angles that correspond to what? Mathematically speaking, you cannot define an angle with a single numeric value. The minimum amount of information that you need to define an angle is three ordered pairs.
I see from your posted code that you have x and y values, and that angle1 is defined based on a function of those x and y values. You do not, however, have a relationship between any particular set of x and y values (i.e. x(1) and y(1) compared against x(2) and y(2)), that we can apply to the z values. Does this mean that you have another function for calculating the angles with z?
'However, elements of z2 matrix has so much fractions which makes finding an angle corresponding to z1+0.2 impossible.'
Why? It is theoretically possible to find an angle from any set of ordered pairs. What other restrictions do you have that make this impossible?
'I want to a solution which can find and place the element, which is closest value to z(i)+0.2 with an error margin 0.05,'
What elements are we comparing here to find this error? If we are comparing the elements of z2 and z1 + 0.2 (I assume that z(i) is a typo), then the error for each corresponding index will be 0.

Sign in to comment.

Answers (1)

Rik
Rik on 18 Apr 2019
Edited: Rik on 18 Apr 2019
Edit:
Now I think I get it. That is also where your precision remark is coming from. The code below gets the job done with ismembertol, although if you look at the plot you might consider sorting the output based on your angle variable (or using a different plot syntax).
xm=6; ym =3:0.01:10;
[y, x] = meshgrid(ym,xm);
angle1 = asind((-x.^2.*y.^2-y.^4+1296)./(2.*x.*y.^3));
angle1 = real(angle1);
z1 = 6./(x.*sind(angle1)+y);
[z2,angle2]=get_z_angle(z1,angle1);
figure(1),clf(1)
plot(z1,angle1,z2,angle2);
%show the test case as well:
z1= [1.153 1.451 1.746 1.349 1.255 1.959 1.550 1.854 1.652 ];
angle1= [10 25 35 20 45 30 15 50 55 ];
z2_test = [ 1.349 1.652 1.959 1.550 1.451 NaN 1.746 NaN 1.854];
angle2_test= [ 20 55 30 15 25 NaN 35 NaN 50];
[z2,angle2]=get_z_angle(z1,angle1);
clc
z2-z2_test
angle2-angle2_test
function [z2,angle2]=get_z_angle(z1,angle1)
z2=z1+0.2;
[hasMatch,matchPos]=ismembertol(z2,z1,0.05,'DataScale',1);
z2(hasMatch)=z1(matchPos(hasMatch));
z2(~hasMatch)=NaN;
angle2=NaN(size(angle1));
angle2(hasMatch)=angle1(matchPos(hasMatch));
end
Original post:
You mean like this? It is unclear to me how you would like to use your example situation without an x and y.
xm=6; ym =3:0.01:10;
[y, x] = meshgrid(ym,xm);
angle1 = asind((-x.^2.*y.^2-y.^4+1296)./(2.*x.*y.^3));
angle1 = real(angle1);
z1 = 6./(x.*sind(angle1)+y);
z2=z1+0.2;
%invert z2 = 6./(x.*sind(angle2)+y); algebraically:
%
% z1+0.2 = 6./(x.*sind(angle2)+y);
% x.*sind(angle2)+y = 6./(z1+0.2);
% x.*sind(angle2) = 6./(z1+0.2) -y;
% sind(angle2) = (6./(z1+0.2) -y)./x;
% angle2 = asind((6./(z1+0.2) -y)./x);
%
angle2 = asind((6./(z1+0.2) -y)./x);
%replace invalids with NaN
invalid=abs(imag(a))>2*eps;
angle2 = real(angle2);angle2(invalid)=NaN;
figure(1),clf(1)
plot(z1,angle1,z2,angle2);
  5 Comments
Adam Danz
Adam Danz on 18 Apr 2019
Ah... maybe! I'm generally reluctant to dive in too deeply until the problem is clearly stated. I think the exersise of describing the problem is as helpful to the OP as it is to us.
@Cem, does Rik's new interpretation describe your goal? Given the values of z1 and angle1, do you want to use those values to determine what angle1 would be for values of Z1+0.2?
Rik
Rik on 24 Apr 2019
@Cem, did this solve your issue? If not, feel free to comment with your remaining issues. If it did solve your issue, please consider marking it as accepted answer.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!