Is there the possibility to discretize data comparing them with a function?
1 次查看(过去 30 天)
显示 更早的评论
Hello everyone, I am creating a matrix with 2 rows and several columns. I would like to compare the pairs of values of this matrix with another matrix that contains other pairs of numbers. The pairs of the second matrix derive them from a function.
The code is: Fr_2=0:0.01:5; r_theoric=zeros(1,length(Fr_2)); for i=1:length(Fr_2) r_theoric(i)=sqrt(27*((Fr_2(i)^2)/((2+Fr_2(i)^2)^3))); end R=[r_theoric;Fr_2]
The resulting curve has 3 "zones" (left, below the curve, right of the curve)
Basically, I would like the first couple to be discretized and I would like Matlab to tell me if these are on the left, below or to the right of the curve. Then.. How can I compare the pairs of the first matrix with the R matrix?
0 个评论
采纳的回答
Star Strider
2018-7-25
Without your second matrix, it is necessary to create data to test code.
It is necessary to ‘close’ the polygon created by the curve so that the inpolygon function will work. I make no guarantees that this will work with your matrix, so you will have to experiment with that.
This works with my test data:
Fr_2=0:0.01:5;
r_theoric=zeros(1,length(Fr_2));
for i=1:length(Fr_2)
r_theoric(i)=sqrt(27*((Fr_2(i)^2)/((2+Fr_2(i)^2)^3)));
end
R=[r_theoric;Fr_2];
R(:,end+1) = [R(1,1); R(2,end)]; % <— Necessary To Complete The Polygon
[Rmax,idx] = max(R,[],2);
another_matrix = bsxfun(@times, rand(50, 2), [5 1]); % Create Data
Below = inpolygon(another_matrix(:,1), another_matrix(:,2), R(2,:), R(1,:)); % Logical Vector
Left = (another_matrix(:,1) < R(2,idx(1))) & ~Below; % Logical Vector
Right = ~Below & ~Left; % Logical Vector
figure
plot(R(2,:), R(1,:))
hold on
plot(another_matrix(Below,1), another_matrix(Below,2), 'pb')
plot(another_matrix(Left,1), another_matrix(Left,2), 'pg')
plot(another_matrix(Right,1), another_matrix(Right,2), 'pm')
hold off
grid
The plot may not be required for your code. It demonstrates that the code works here.
2 个评论
Star Strider
2018-7-27
Gianluca Borgna’s ‘Answer’ moved here:
It was exactly what i wanted. Thanks. Now i have to understand how to put my data instead of yours.
Star Strider
2018-7-27
My pleasure.
If you attach your data, preferably as a text file, I can probably help you with that.
Also, if my Answer helps you solve your problem, please Accept it!
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!