Is there a way to assign a variable-dimension calculation to an array of generic dimension?

1 次查看(过去 30 天)
I am creating two functions, I'll refer to them as "A" and "B", that each call a third function I'll refer to as "C".
Function C performs an array calculation on an inputted array of any dimension. The output dimension needs to depend on the input dimension. (See MWE below).
Function A inputs a vector into C, Function B inputs a matrix into C. Because these are used in simulation of a system (i.e. called many times), I would like to avoid an "if-statement" in "Cfunction". Is there a way to generically assign the calcuations to Output so that its dimension is determined by the dimension of the RHS of the equation?
Something like:
Output(1,~)= 2*Xinput;
Output(2,~)=5*Xinput;
Or if this is not possible, any tips for accomplishing my goal efficiently. Needless to say my real "Cfunction" is far more complex than my example below - hence the desire to avoid errors by calling the same function in A and B.
Thanks!
function Afunction
X=[1 2 3];
D=Cfunction(X)
end
function Bfunction
X = [1 2 3; 4 5 6]
D=Cfunction(X)
end
function Output=Cfunction(Xinput)
if ndims(Xinput)==2
Output(1,:,:)=2*Xinput;
Output(2,:,:)=5*Xinput;
elseif ndims(Xinput)==1
Output(1,:)=2*Xinput;
Output(2,:)=5*Xinput;
end
end
  2 个评论
dpb
dpb 2021-6-30
As written, D will be reallocated to whatever the size() of the array Cfunction returns because there are no subscripting expressions so the whole array is reallocated on assignment.
As far as the sample Cfuncton, ndims() can NEVER return <2 so the elseif clause is never going to be invoked--if the idea is whether the input is a vector or an array, then use isvector(Xinput)
The example is probably too simplified to get at the real use I'm guessing, but if the calling sequence in the consumer of the subject function is actually as written above, then it makes no difference at all because the whole LHS variable is written.
lstr
lstr 2021-6-30
Ah, perhaps I did make the example too simple. I'm not sure the proper etiquitte - if it is better to clarify in the comments or to edit the original question?
In my actual code A inputs a 2-dimensional matrix into Cfunction, and B inputs a 3-dimensional array into Cfunction.
The Cfunction then does something like:
x=X(1,:,:);
y= X(2,:,:);
Output(1,:,:) = 2*x+3*y;
Output(2,:,:) = x-y;
The assignment of x and y seems to work fine regardless of whether X is 2- or 3- dimensional.
But the assignment to Output, understandably, stores the output of the RHS to Output(1,1,:) and Output(2,1,:) if X was 2-dimensional rather than 3-dimensional. i.e. it creates a 3-dimensional "Output" regardless of the size of X.
I'm wondering if there is a way to make sure Output matches the dimension of X.
Does this clarify?

请先登录,再进行评论。

采纳的回答

Matt J
Matt J 2021-6-30
编辑:Matt J 2021-6-30
This will work independently of the dimension of Xinput:
function Output=Cfunction(Xinput)
Output(2,:)=5*Xinput(:);
Output(1,:)=2*Xinput(:);
Output=reshape(Output, [size(Output,1) , size(Xinput)] );
end
  3 个评论
Matt J
Matt J 2021-6-30
Yes. Actually, though, if you have pre-allocated Output (which you probably should do), then the call to reshape() becomes unnecessary.
function Output=Cfunction(Xinput)
Output=nan([2,size(Xinput)]); %pre-allocate
Output(1,:)=2*Xinput(1,:);
Output(2,:)=3*Xinput(2,:);
end

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by