Mapping a cell array on another cell array

1 次查看(过去 30 天)
My objective is to map B on A
A = {[x(2) & x(4)& x(1)],[x(3),x(5)],[x(2),x(1)]}
B = {2465,2514,147,236,58}
where the integers in A are the indices of items in B. I want to replace those indices with actual values as:
C = {[x(2514) & x(236) & x(2465)],[x(147),x(58)],[x(2514),x(2465])}
For loop is a bit slow. Is there an alternative?
  2 个评论
madhan ravi
madhan ravi 2020-6-4
How does this differ from your previous question?
Ronald
Ronald 2020-6-4
removing the formating from the output does not serve the purpose for which I need it for. Can you solve it without removing the other characters as in my intended output?

请先登录,再进行评论。

回答(1 个)

Walter Roberson
Walter Roberson 2020-6-4
No, there is no alternative for that situation. That situation cannot be handled at all, except for the case where x is a vector of unique symbolic variables.
When you do
A = {[x(2) & x(4)& x(1)],[x(3),x(5)],[x(2),x(1)]}
then MATLAB looks up the contents of x(2) and x(4) and so on, and puts the contents into the expression, applying operators as it goes, so x(2) would be replaced with the contents of what is in the second location in x, and then x(4) would be replaced with the contents of what is in the 4th location in x, and then the & operator would be applied between the two values, reducing down to a single value; and then the contents of the first location of x would be looked up and the & operator would be applied between the result of the first & and the x(1), reducing down to a single value. Your A would come out as a cell array in which the first element had one value, the second element had two values, and the third element had two values. MATLAB throws away all information about how the values are derived as soon as the individual value has been produced. After the [x(2) & x(4) & x(1)] operation, MATLAB has no memory that the resulting scalar value has anything to do with x(2) .
Non-symbolic expressions that are not function handles are not patterns of computation. You cannot manipulate the form of how a value was calculated because all information about that has been forgotten.
If you were using function handles, then you could func2str() and do character processing and str2func(). Not so pleasing, but could be done relatively compactly using careful regexprep.
If you were using symbolic expressions and the x values each hold unique symbolic values, then you can use subs().
Note: symbolic expressions can re-arrange themselves.
>> 3 + y*x + 2
ans =
x*y + 5
If the order is important for whatever reason, if it is important that parts not be folded together, then symbolic expressions might not be right for you.
  3 个评论
Walter Roberson
Walter Roberson 2020-6-5
Afun = @(x) {[x(2) & x(4)& x(1)],[x(3),x(5)],[x(2),x(1)]};
B = {2465,2514,147,236,58};
Bmap = containers.Map(string(1:length(B)), string(B));
new_Afun = str2func(regexprep(func2str(Afun), '(?<=x\()(\d+)(?=\))', '${Bmap($1)}'));
Ronald
Ronald 2020-6-5
Thank you Walter. I made some few regex modifcations on the output and got exactly what I was looking for!

请先登录,再进行评论。

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by