- Technical Services and Consulting
- Embedded Systems | Firmware Developement | Simulations
- Electrical and Electronics Engineering
vectorized operations on symbolic functions
8 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Muhammad Uzair
el 19 de Feb. de 2024
Hi, Can we apply vectorized operations on symbolic functions to avoid loops?
syms x1 x2 x3; % symbolic variables
y = x1^3/3 + x2^2/2 - x3; % symbolic function y
X = rand(500,3) % each row representing a combination of x1 x2 x3
I used for loop, but it is taking more time.
y_values = zeros(size(X,1))
for ii = 1:size(X,1)
y_values(ii) = subs(y, [x1, x2, x3], X(ii,:));
end
I have tried following but since there is inconsistency between sizes of old and new it don't work for me.
y_values = subs(y, [x1, x2, x3], X); % Evaluate the function for all combinations in matrix X
Thanks
0 comentarios
Respuesta aceptada
Hassaan
el 19 de Feb. de 2024
Editada: Hassaan
el 19 de Feb. de 2024
syms x1 x2 x3; % symbolic variables
y = x1^3/3 + x2^2/2 - x3; % symbolic function y
% Generate a random matrix X with 500 rows and 3 columns
X = rand(500, 3); % each row representing a combination of x1 x2 x3
% Evaluate the symbolic function y for each row in X
% Convert the matrix X to a cell array where each row is a separate cell
X_cell = num2cell(X, 2);
% Use cellfun to apply the subs function to each cell (row) of X_cell
y_values = cellfun(@(row) subs(y, [x1, x2, x3], row), X_cell);
% Convert y_values to a double array if needed
y_values = double(y_values);
disp(y_values(1:10))
-----------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
It's important to note that the advice and code are based on limited information and meant for educational purposes. Users should verify and adapt the code to their specific needs, ensuring compatibility and adherence to ethical standards.
Professional Interests
Feel free to contact me.
2 comentarios
Dyuman Joshi
el 21 de Abr. de 2024
Note that using num2cell and subsequently cellfun is not efficient.
Más respuestas (5)
Matt J
el 19 de Feb. de 2024
Editada: Matt J
el 19 de Feb. de 2024
syms x1 x2 x3; % symbolic variables
y = x1^3/3 + x2^2/2 - x3; % symbolic function y
X = rand(500,3); % each row representing a combination of x1 x2 x3
ynum=matlabFunction(y) %convert to a numeric function
yvalues=ynum(X(:,1),X(:,2),X(:,3))
0 comentarios
Dyuman Joshi
el 19 de Feb. de 2024
Better to use a function handle -
x = rand(500,3);
y = @(x) x(:,1).^3/3 + x(:,2).^2/2 - x(:,3);
out1 = y(x)
For more information, refer to these documentation pages -
2 comentarios
Christine Tobler
el 19 de Feb. de 2024
Your best approach then might be to make your input variables have the individual pages already as a third dimension:
x = randn(3, 1, 500);
y = randn(1, 6, 500);
z = x.*y;
size(z)
The formula here is of course completely made up, it would depend on what your formula for this 3x6 matrix is.
Aquatris
el 19 de Feb. de 2024
Editada: Aquatris
el 19 de Feb. de 2024
Yet another method, you can define your symbolic parameter as a matrix symbolic
n = 500;
X = sym('X', [500 3]);
% I assume here you want a element wise power ('.^') instead of matrix power
y = X(:,1).^3/3 + X(:,2).^2/2 - X(:,3); % symbolic function y
X_val = rand(500,3); % each row representing a combination of x1 x2 x3
y_values = double(subs(y, X, X_val)) % Evaluate the function for all combinations in matrix X
size(y_values)
0 comentarios
Walter Roberson
el 19 de Feb. de 2024
syms x1 x2 x3; % symbolic variables
y = x1^3/3 + x2^2/2 - x3; % symbolic function y
X = rand(500,3); % each row representing a combination of x1 x2 x3
y_values = double(subs(y, {x1, x2, x3}, {X(:,1), X(:,2), X(:,3)}));
y_values(1:5)
0 comentarios
Torsten
el 21 de Abr. de 2024
Editada: Torsten
el 21 de Abr. de 2024
rng("default")
syms x1 x2 x3; % symbolic variables
y = x1^3/3 + x2^2/2 - x3; % symbolic function y
X = rand(500,3) % each row representing a combination of x1 x2 x3
y_values = arrayfun(@(i)double(subs(y,[x1,x2,x3],[X(i,1),X(i,2),X(i,3)])),1:size(X,1))
0 comentarios
Ver también
Categorías
Más información sobre Calculus en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!