Rebuild the matrix according to the values of its elements

조회 수: 1 (최근 30일)
Cem Tuncer
Cem Tuncer 2019년 4월 17일
댓글: Rik 2019년 4월 24일
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
Bob Thompson
Bob Thompson 2019년 4월 18일
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.

댓글을 달려면 로그인하십시오.

답변 (1개)

Rik
Rik 2019년 4월 18일
편집: Rik 2019년 4월 18일
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
Adam Danz
Adam Danz 2019년 4월 18일
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 2019년 4월 24일
@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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by