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 ビュー (過去 30 日間)
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.

採用された回答

jgg
jgg 2015 年 12 月 14 日
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 件のコメント
Tien Phat Ngo Nguyen
Tien Phat Ngo Nguyen 2015 年 12 月 15 日
Thank you jgg. It's worked as expected, and the code you wrote is easy to understand too. Thanks again.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by