1 view (last 30 days)

Greetings!

I would like your help on a function I'd like to make on Matlab.

I want to make a function called R,

[Roots] = R(X,Y,eps) and this function will be returning a table with 2 columns showing the X and Y of the points where abs(Y)<eps

Thank you in advance.

Image Analyst
on 1 Dec 2019

Try this (untested)

% This function will be returning a table with 2 columns showing the X and Y of the points where abs(Y)<eps

function [Roots] = R(X,Y,eps)

Roots = []; % Initialize to no values.

indexes = abs(Y) < eps;

if isempty(indexes)

% No Y satisfies the criteria.

return;

end

xSmall = reshape(X(indexes), [], 1); % Extract indexes and make sure they're in a column vector.

ySmall = reshape(Y(indexes), [], 1); % Extract indexes and make sure they're in a column vector.

Roots = table(xSmall, ySmall);

end

dpb
on 1 Dec 2019

Continuing w/ Walter's explanation, because if row vectors,

>> X=1:10;Y=randn(size(X));

>> table(X,Y)

ans =

1×2 table

X Y

_____________ _____________

[1x10 double] [1x10 double]

>>

you see you get a row vector for each of the two variables as the one record in the table...this probably is not what is wanted.

Image Analyst
on 1 Dec 2019

Sign in to comment.

Dhmhgr
on 1 Dec 2019

Image Analyst
on 1 Dec 2019

I didn't notice at first that we were using eps. That is probably a bad thing to use (unless it's a trick question) because no value of Y can be less than eps unless it's pure zero.

Try this (with a variable named minValue):

X = rand(1, 100);

Y = rand(1, 100);

minValue = 0.1;

Roots = R(X,Y,minValue)

% This function will be returning a table with 2 columns showing the X and Y of the points where abs(Y)<eps

function [Roots] = R(X,Y,minValue)

Roots = []; % Initialize to no values.

indexes = abs(Y) < minValue;

if isempty(indexes)

% No Y satisfies the criteria.

return;

end

xSmall = reshape(X(indexes), [], 1); % Extract indexes and make sure they're in a column vector.

ySmall = reshape(Y(indexes), [], 1); % Extract indexes and make sure they're in a column vector.

Roots = table(xSmall, ySmall);

end

You'll see

Roots =

11×2 table

xSmall ySmall

_________________ ___________________

0.970592781760616 0.0838213779969326

0.792207329559554 0.0781755287531837

0.849129305868777 0.00463422413406744

0.743132468124916 0.0844358455109103

0.489764395788231 0.0759666916908419

0.679702676853675 0.0496544303257421

0.890903252535798 0.0964545251683886

0.257508254123736 0.0597795429471558

0.243524968724989 0.0154034376515551

0.929263623187228 0.0430238016578078

0.567821640725221 0.0811257688657853

Sign in to comment.

Dhmhgr
on 2 Dec 2019

Edited: Walter Roberson
on 2 Dec 2019

Image Analyst
on 2 Dec 2019

Sign in to comment.

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 4 Comments

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/494031-help-on-a-function#comment_772918

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/494031-help-on-a-function#comment_772918

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/494031-help-on-a-function#comment_772928

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/494031-help-on-a-function#comment_772928

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/494031-help-on-a-function#comment_772957

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/494031-help-on-a-function#comment_772957

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/494031-help-on-a-function#comment_773157

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/494031-help-on-a-function#comment_773157

Sign in to comment.