Mapping a cell array on another cell array

2 visualizaciones (últimos 30 días)
Ronald
Ronald el 4 de Jun. de 2020
Comentada: Ronald el 5 de Jun. de 2020
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 comentarios
madhan ravi
madhan ravi el 4 de Jun. de 2020
How does this differ from your previous question?
Ronald
Ronald el 4 de Jun. de 2020
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?

Iniciar sesión para comentar.

Respuestas (1)

Walter Roberson
Walter Roberson el 4 de Jun. de 2020
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 comentarios
Walter Roberson
Walter Roberson el 5 de Jun. de 2020
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 el 5 de Jun. de 2020
Thank you Walter. I made some few regex modifcations on the output and got exactly what I was looking for!

Iniciar sesión para comentar.

Community Treasure Hunt

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

Start Hunting!

Translated by