如何创建3d符号张量?

时间:2013-04-02 16:23:08

标签: matlab

这有效

A = sym('A%d%d', [2 4])

A =

[ A11, A12, A13, A14]
[ A21, A22, A23, A24]

但是这会给出错误

A = sym('A%d%d%d', [2, 4 ,6])
Error using sym>createCharMatrix (line 2001)
Matrix syntax can only create vectors and
matrices.

Error in sym>convertCharWithOption (line 1960)
        s = createCharMatrix(x,a);

Error in sym>tomupad (line 1693)
        S = convertCharWithOption(x,a);

Error in sym (line 113)
            S.s = tomupad(x,a);

如何在matlab中创建符号3d张量?

1 个答案:

答案 0 :(得分:3)

合理的方法是创建字符串的单元格数组并将其转换为符号变量:

%// Create matrices of indices
[k1 k2 k3] = ndgrid(1:2, 1:4, 1:6);

%// Create a cell array of strings
C = regexp(sprintf('A%d%d%d', [k1(:) k2(:) k3(:)]'), 'A\d{3}', 'match');

%// Convert strings to symbolic objects and convert back into a matrix
A = cell2mat(cellfun(sym, reshape(C, size(k1)), 'UniformOutput', 0));