Unknown number of output variables

조회 수: 31(최근 30일)
Mohammad Abouali
Mohammad Abouali 2016년 1월 15일
편집: Mohammad Abouali 2016년 1월 15일
There are some functions that support variable number of output variables. For example, ind2sub(), depending on the matrix size, could have variable number of output.
if I type:
[I1,I2]=ind2sub([3,4],10);
I get the correct answer, I1=1 and I2=4;
but if I issue:
subIndices=ind2sub([3,4],10);
then I won't get subIndices= [1,4]; but I get subIndices=10; which definitely is not what I wanted.
Now here is my quesitons. What if I don't know the input size and that is defined during the runtime, meaning that my input size once might be [3,4] and another time it would be [3,4,5]. So once I need to call [I1,I2]=ind2sub([3,4],10) but another time I need to have [I1,I2,I3]=ind2sub([3,4,5],10) in the code.
In this easy case I just rewrote another version of ind2sub which outputs the indices as vector, you can see the code at the end of this post; but what is a better way of doing this?
P.S. I don't want to construct the command string and use eval()
Here is the modified version of ind2sub():
function subIndices = myInd2Sub(inputSize,index)
%%myInd2Sub is similar to MATLAB's ind2sub except that the output, i.e.
% the indices for each dimensions, are provided in a single array, instead
% of separate output variables.
% Checking input arguments
validateattributes(inputSize, ...
{'numeric'}, ...
{'vector','integer','positive'});
validateattributes(index, ...
{'numeric'}, ...
{'scalar','integer','positive','<=',prod(inputSize)});
% preparing the output
subIndices = zeros(numel(inputSize),1);
% changing the index to zero-base
index = index - 1;
% calculating the indices for each dimension
for i=numel(subIndices):-1:2
nElem = prod(inputSize(1:i-1));
subIndices(i) = floor(index/nElem);
index = index - subIndices(i)*nElem;
end
subIndices(1) = index;
% changing back the indices to 1-base.
subIndices = subIndices + 1;
end

채택된 답변

Steven Lord
Steven Lord 2016년 1월 15일
편집: Steven Lord 2016년 1월 15일
% User specifies number of inputs/outputs at runtime
N = input('Enter number of dimensions to mesh (2-5) ');
% Inputs to NDGRID specified at runtime by user as elements of a cell array
inputs = repmat({[0,1]}, 1, N);
% Outputs from NDGRID specified at runtime by user as elements of a cell array
outputs = cell(1, N);
% Call NDGRID with specified number of inputs and outputs by turning cell arrays into CSLs
[outputs{:}] = ndgrid(inputs{:});
% Use the outputs to display the coordinates of one of the points on the grid
d = input(sprintf('Enter an integer value between 1 and %d ', 2^N));
fprintf('Your combination is:')
for k = 1:N
fprintf(' %d', outputs{k}(d));
end
fprintf('\n')
I didn't add in any error checking for the values of N or d because this is a very quick example. But I wouldn't use too large an N as it could use a lot of memory.
  댓글 수: 1
Mohammad Abouali
Mohammad Abouali 2016년 1월 15일
Great. That's exactly what I wanted. Thanks.

댓글을 달려면 로그인하십시오.

추가 답변(0개)

Community Treasure Hunt

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

Start Hunting!

Translated by