putting a sequence of text values into a multivariable function.
조회 수: 1 (최근 30일)
이전 댓글 표시
The problem is easy, but i dont find the solution.
Lets say we want combine some vectors: a1, a2, a3.
a1 = [1,2,3];
a2 = [21,22,23];
a3 = [31,32];
Comb = combvec(a1,a2,a3);
for that we can use the function Comb = combvec(a1,a2,a3). So the solution is:
a =
1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3
21 21 21 22 22 22 23 23 23 21 21 21 22 22 22 23 23 23
31 31 31 31 31 31 31 31 31 32 32 32 32 32 32 32 32 32
which is the vector of all the posible combinations.
Now, lets supouse that there is a case where we dont know how many vectors we have ( they are generated while running ).
However, we can get a char with the name of those vectors like:
n = 3;
final=[];
for k=1:3
if n >= 2
Data = ['a' num2str(k) ','];
n=n-1;
else
Data = ['a' num2str(k)];
end
final=strcat(final,Data);
end
Comb = combvec(final).
And the result of this is operation is a single char: final = ('a1,a2,a3');
But if we try to insert this array into the function like: Comb = combvec(final). We will get this result:
a =
'a1,a2,a3'
Can someone help me to solve it?
Thanks
댓글 수: 0
답변 (1개)
Shanmukha Voggu
2021년 12월 1일
Hi Jamie,
I understood that you want to process a char that contains the variable names of matrices separated by commas
a1 = [1,2,3];
a2 = [21,22,23];
a3 = [31,32];
% The code below assumes the variable names mentioned in "inputString"
% are already inside the workspace
inputString='a1,a2,a3';
matrixNames=split(inputString,",")% split the string using delimiter as comma
answer=eval(matrixNames{1});% initialize the answer variable
for i = 2:length(matrixNames)
secondMatrix=eval(matrixNames{i});% evaluate the expression
answer=combvec(answer,secondMatrix);
end
answer
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!