So I'm trying to dynamically solve a matrix and have it so I can reuse it next loop however solve seems to return a structure that I can't dynamically cast back into a matrix.
The only code I've found to do it dynamically fails for reason "CELL2MAT does not support cell arrays containing cell arrays or objects."
%Matlab solver test
NumberOfMasses = 20;
lamda = sym('lamda',[NumberOfMasses NumberOfMasses]);
test = lamda * eye(NumberOfMasses,NumberOfMasses);
eqn = lamda * eye(NumberOfMasses,NumberOfMasses) == eye(NumberOfMasses,NumberOfMasses)*2;
ans = solve(eqn,lamda);
cell2mat(struct2cell(ans))%needs to be a matrix for next loop
Is there something I'm missing to Dynamically cast "ans" back to a matrix? (Preferably without writing code that dynamically builds strings and runs getfield() on the struct)

 Respuesta aceptada

madhan ravi
madhan ravi el 1 de Sept. de 2019

4 votos

Why not use struct2array() ? Note: Don’t Name a variable named ans .

5 comentarios

Dion Richardson
Dion Richardson el 1 de Sept. de 2019
Editada: Dion Richardson el 1 de Sept. de 2019
Ahhh thanks, completely missed it. Only issue now is that I have to reshape it every time. Is there anything else I'm missing because this seems like a decent work around. (Just a lot of redundancy in larger code bases and it also fails if the matrix gets turned into a scalar during development)
reshape(struct2array(anz),[NumberOfMasses NumberOfMasses])
I just noticed the warning on ans after I posted hahaha
madhan ravi
madhan ravi el 1 de Sept. de 2019
I don’t see any alternatives at the moment, if you get the desired result using reshape() then why not use it, anyhow you’re willing to use it in a loop??
>> anz = solve(eqn,lamda);
>> anz
anz =
struct with fields:
lamda1_1: [1×1 sym]
lamda2_1: [1×1 sym]
lamda3_1: [1×1 sym]
lamda4_1: [1×1 sym]
lamda5_1: [1×1 sym]
lamda6_1: [1×1 sym]
lamda7_1: [1×1 sym]
lamda8_1: [1×1 sym]
lamda9_1: [1×1 sym]
lamda10_1: [1×1 sym]
lamda11_1: [1×1 sym]
lamda12_1: [1×1 sym]
lamda13_1: [1×1 sym]
lamda14_1: [1×1 sym]
lamda15_1: [1×1 sym]
lamda16_1: [1×1 sym]
lamda17_1: [1×1 sym]
lamda18_1: [1×1 sym]
lamda19_1: [1×1 sym]
lamda20_1: [1×1 sym]
lamda1_2: [1×1 sym]
Look at the order of the fields: all of the _1 appear then all of the _2 and so on. Is that the order that you want the matrix to show up as?
I suggest that instead of converting to array, that you use:
subs(lambda, anz)
Dion Richardson
Dion Richardson el 1 de Sept. de 2019
Editada: Dion Richardson el 1 de Sept. de 2019
I'm prototyping mathematical functions that I want to later run in C++. My issue is I'm not great at maths so I often make mistakes and change parts of my formula. This occasionally means that in some cases weird conditions come up. The loop time is about 20-30 seconds for some of my code (which is fine for prototyping).
Subs works perfectly! Thanks so much, never would've expected that to be the case.
madhan ravi
madhan ravi el 1 de Sept. de 2019
Thank you sir Walter!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Mathematics en Centro de ayuda y File Exchange.

Productos

Versión

R2019a

Preguntada:

el 1 de Sept. de 2019

Comentada:

el 1 de Sept. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by