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

1 Ansicht (letzte 30 Tage)
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 Kommentare
dpb
dpb am 30 Jun. 2021
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 am 30 Jun. 2021
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?

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Matt J
Matt J am 30 Jun. 2021
Bearbeitet: Matt J am 30 Jun. 2021
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 Kommentare
Matt J
Matt J am 30 Jun. 2021
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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Resizing and Reshaping Matrices finden Sie in Help Center und File Exchange

Produkte


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by