필터 지우기
필터 지우기

Automatically creating an array of variables and assigning to a function with changing number of outputs

조회 수: 26 (최근 30일)
I am writing a script that creates an array (A) of vectors. The value of each vector is the set subscripts that describe it's position within the array. eg, for a three dimensional square array with side length 4:
A{1} = [1,1,1]
A{4} = [4,1,1]
A{9} = [1,1,3]
Each vector is then passed to another function which calculates my final result. eg,
results(i)=myfunction(A(i))
Im trying to automate the first stage, ie, assigning the value of the vector A{i} to be its subscript place within A. This will be variable on both the number of dimensions that A has and the side length (the sides are always the same length).
The method I have devised involves using ind2sub(). The number of outputs from in2sub is dependent on the dimensionality of the input matrix. To overcome this I am creating a character array with a variable for each dimension of A. eg for 3 dimensions.
vector_name='[x,y,z]';
and then concatenate it with the string '=ind2sub(size(results),i)' :
command_sting=strcat(vector_name,'ind2sub(size(results),i)')
so for my 3 dimension example command_string would store the character array:
command_string=
'[x,y,z]=ind2sub(size(results),i)'
I am then trying to use eval() to evaluate this statement but I get an error message:
eval(command_string)
Index exceeds matrix dimensions.
However if I type what is stored in command_string straight into the command line I do get the right result:
i=1
[x,y,z]=ind2sub(size(results),i)
x =
1
y =
1
etc.
Why is the eval(command _string) not working? If anyone has a more elegant solution than this use of eval() I'd be grateful to hear it.
  댓글 수: 3
Stephen23
Stephen23 2017년 1월 31일
편집: Stephen23 2017년 1월 31일
"a more elegant solution"
Copy ind2sub and change it to output a vector. It is quite a simple change.

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

채택된 답변

Guillaume
Guillaume 2017년 1월 31일
편집: Guillaume 2017년 1월 31일
You don't need to use eval to cope with varying number of outputs. Use cell arrays and their implicit conversion to comma-separated lists:
indices = cell(1, ndims(results)); %create a cell array the right size
[indices{:}] = ind2sub(size(results), someindex); %assign c-s-l output to cell array
indexvector = [indices{:}]; %convert cell array to vector.
  댓글 수: 3
Stephen23
Stephen23 2017년 1월 31일
편집: Stephen23 2017년 1월 31일
Note that ind2sub is a very simple function, and it is trivial to copy it and make your own version that returns a vector instead of separate output arguments (do NOT overwrite the original file).
Then you could avoid the whole bother altogether.

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

추가 답변 (1개)

Adam
Adam 2017년 1월 31일
[x, y, z] = ind2sub( [3,4], 9 );
works even though the size of the input is 2d, you just get z = 1. Likewise if you use n outputs you will just get 1 for those beyond the dimensionality of the input.
I would prefer this as an implementation personally, using the maximum dimensionality you will have. Possibly it may be better putting the results directly into an array rather than n named variables, but wither works.
Your code that comes after your eval would still have the problem of having to know what variables it needs to put into the A cell array so you might as well have all the variables there and ignore the ones not relevant for the given dimensionality.
  댓글 수: 2
Bob Hickish
Bob Hickish 2017년 1월 31일
Thanks for your answer. This would solve my problem, however I think the other solution above is a bit more elegant as it is generic without having unused variables hanging around.
Adam
Adam 2017년 1월 31일
Yes, I would agree. I've never tended to use comma-separated lists for these kind of things so I am always unfamiliar with where they can be used for assignments.

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

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by