how to "write" a equation like string from a number matrix and a vector of varibles, and put those equation into cell to slxwrite to excel ?

2 vues (au cours des 30 derniers jours)
I have a vector of name of variables like 'cake' 'sandwich' 'key' 'sun' etc, and a matrix of n x m coeff. I need to combine each row of the matrix with the vector (sort of like what a bsxfun(@times,matrix, vector) do if the vector is numeric). They should become a vector of n equations like 0.01*cake-0.5*sandwich+10*key (with no space and the '*' character included). I've been searching a lot, but with no proper knowledge of matlab I can't find anything. Noted the matrix is taken from a system of linear equations, but if I have to write them all back is would be fruitless since the matrix usually huge and there are many of them :(. And slxwrite them back into excel too. Please help me.

Réponse acceptée

jgg
jgg le 14 Déc 2015
Something like this should do it:
A = magic(2);
c = cell(1,2);
c{1} = 'cat';
c{2} = 'winter';
out = cell(2,1);
for j = 1:2
str = '';
for i = 1:2
if i < 2
str = strcat(str,mat2str(A(j,i)),'*',c{i},'+');
else
str = strcat(str,mat2str(A(j,i)),'*',c{i});
end
end
out{j} = str;
end
You'll have to adjust the code a little bit so it works with your data, but basically just concatenate the strings.
  1 commentaire
Tien Phat Ngo Nguyen
Tien Phat Ngo Nguyen le 15 Déc 2015
Thank you jgg. It's worked as expected, and the code you wrote is easy to understand too. Thanks again.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by