Using subs on contents of a cell

6 vues (au cours des 30 derniers jours)
iamShreyans
iamShreyans le 12 Nov 2019
Modifié(e) : iamShreyans le 17 Nov 2019
I have a cell 'I' which is defined as,
I = {I1,I2};
where, I1 and I2 are syms variables
At some point later, I wish to substitute the values of both I1 and I2 with a sym 4x4 matrix named I_val. For this, I wrote the following code,
I = subs(I,[I1,I2],[I_val,I_val]);
But I'm getting the following error,
Error using subs
Expected input number 1, S, to be one of these types:
sym
Instead its type was cell.
Error in sym/subs (line 60)
validateattributes(F, {'sym'}, {}, 'subs', 'S', 1);
I don't understand which at which part I'm making mistake. I even tried by converting cell 'I' to a sym before using subs but this sill doesn't seem to work
I = subs(cell2sym(I),[I1,I2],[I_val,I_val]); %Converting cell 'I' to sym
%The error that I'm getting
Error using sym/subs>normalize (line 226)
Inconsistency between sizes of second and third arguments.
Error in sym/subs>mupadsubs (line 157)
[X2,Y2,symX,symY] = normalize(X,Y); %#ok
Error in sym/subs (line 145)
G = mupadsubs(F,X,Y);

Réponse acceptée

Ajay Pattassery
Ajay Pattassery le 15 Nov 2019
You can substitute I1 with I_val and I2 with I_val seperately.
I = subs(cell2sym(I),I1,I_val);
But once you do substitution with I1, the I matrix will be resized automatically since your I_val is of different dimension comapred to I1. Hence you need to look at the resultant I matrix and need to change I_val to get required result.
  1 commentaire
iamShreyans
iamShreyans le 17 Nov 2019
Modifié(e) : iamShreyans le 17 Nov 2019
Thank you for taking out your time to answer this!
I would actually need to call I{1} and I{2} later in my calculation in place of I1 and I2, so converting I from cell to sym matrix would make it impossible to do so.
Also, going by your method, I'll have to check my 'I' matrix at each step. I was hoping to get a way such that user has to just enter I_val manually and the subs part would be done automatically. Later if the contents of cell I increase from just 2 to 4 or 5, it would create lots of problem I guess.
Edit: I just found that this answer is pretty simple actually. Even for a larger size of cell I, the solution can be coded to an extent that user does not have to do anything manually for substitution.

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by