I notice if we want to assign a vector to several variable we can use 'deal'; but when the assigning a matrix to several vectors, it doesn't work out. For example
A=[1 2; 3 4];
A=num2cell(A);
[a, b]=deal(A{:})
It gives a error message "Error using deal (line 38) The number of outputs should match the number of inputs."
Do you know how to improve the code? Thank you!!
You can write your own deal very easly:
# in mydeal.m
function varargout = mydeal(varargin)
% Assign values in vector into variables.
%
% EXAMPLE 1
% [a,b,c] = mydeal([1,2,3]);
% EXAMPLE 2
% some_vector = [1,2,3];
% [a,b,c] = mydeal(some_vector);
%
% %results in a=1, b=2, c=3;
%
assert(nargout == size(varargin{1}, 2), 'Different number of in and out arguments');
for i = 1:nargout
varargout{i} = varargin{1}(:, i);
end
For example:
>> [a,b] = mydeal([1 2; 3 4])
a =
1
3
b =
2
4
Or
>> [a,b, c] = mydeal([1 2 3])
a =
1
b =
2
c =
3