I have two float variables:
x = 0.5;
y = 1.5;
I would like to floor them:
x = floor(x);
y = floor(y);
Can i do it in a single command? this raises an error:
[x y] = floor([x y]);
You can write your own wrapper for floor:
function varargout = myFloor(varargin)
for k = 1:nargin
varargout{k} = floor(varargin{k});
end
This function shows the required behavior if you provide x and y as two separated arguments
[a, b] = myFloor(x,y)
This results in
a =
0
b =
1
If you want to use the concatenated array [x y]
as input (like in your example) you can use the following function:
function varargout = myFloor(x)
for k = 1:numel(x)
varargout{k} = floor(x(k));
end
Then you would call
[a, b] = myFloor([x y])
This results in
a =
0
b =
1
Just adding a random idea here...
Building on H.Muster solution, you might want to define a personalized deal
function, which is like deal
but also applies a function to each argument:
function varargout = myDeal(fun, varargin)
if nargin == 2
varargout(1:nargout) = {feval(fun, varargin{1})};
elseif nargin-1 == nargout
for k = 1:nargout
varargout{k} = feval(fun, varargin{k}); end
else
error('Argument count mismatch.');
end
end
This way, you can multi-assign any function:
>> [x,y,z] = myDeal(@floor, 0.5, 0.6, 2.7)
x =
0
y =
0
z =
2
>> [x,y,z] = myDeal(@sin, pi/6)
x =
4.999999999999999e-01
y =
4.999999999999999e-01
z =
4.999999999999999e-01
>> [a, b] = myDeal(@fix, 10*rand(2), 8*rand(5))
a =
7 2
7 6
b =
5 2 4 1 6
1 4 5 1 1
0 1 7 2 7
3 6 7 6 2
7 2 4 2 1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With