how can i substitute elements of a symbolic matrix with arrays of numbers?
2 次查看(过去 30 天)
显示 更早的评论
Hi,
i have a symbolic matrix
A(2*3) = [a_1 a_2 a_3; a_4 a_5 a_6];
also i have 20*1 arrays of numbers. such as
b_1(20,1),b_2(20,1),b_3(20,1),b_4(20,1),b_5(20,1).
i need to substitute elements of symbolic matrix with elements of array one at a time, lets say
a_1=b_1(1,1),a_2=b_2(1,1),a_3=b_3(1,1),a_4=b_4(1,1),a_5=b_5(1,1)
and keep substituting to the last elements of a_1=b_1(20,1), ....
can someone please give me a hint?
thank you in advance
采纳的回答
Walter Roberson
2017-1-2
arrayfun(@(K) subs(A, A, [b_1(K), b_2(K), b_3(K); b_4(K), b_5(K), b_6(K)]), 1:length(b_1), 'Uniform', 0)
Note that the substituting vector, [b_1(K), b_2(K), b_3(K); b_4(K), b_5(K), b_6(K)] must be the same shape as the vector of variables, A, for this to work.
An easier way to handle this would be
mat2cell( reshape([b_1, b_2, b_3, b_4, b_5, b_6], [], 3, 2), ones(1, length(b_1)), 3, 2 )
since really your A is not participating in the process.
5 个评论
Walter Roberson
2017-1-3
A will not be a cell array, but the output of the arrayfun will be a cell array. Do not confuse things by assigning it to the same variable name.
You can access members of the cell array using {} indexing.
subs_A = arrayfun(@(k) subs(A, A, [J_up_00(k), S(k), N_up_00(k); omega_up_00(k), sigma_00_up(k), lambda_up_00(k)]), 1:length(J_up_00), 'Uniform', 0);
for K = 1 : numel(subs_A)
this_A = subs_A{K};
c = b * this_A;
not sure what you want to do with c
end
But it sort of sounds to me as if you want
subs_A = arrayfun(@(k) subs(A, A, [J_up_00(k), S(k), N_up_00(k); omega_up_00(k), sigma_00_up(k), lambda_up_00(k)]), 1:length(J_up_00), 'Uniform', 0);
c = cellfun( @(this_A) b * this_A, subs_A, 'Uniform', 0);
Walter Roberson
2017-1-3
If you have multiple cell arrays that you want to multiply corresponding values of, then for example
c = cellfun( @(this_A1, this_A2, this_A3) b * this_A, subs_A1, subs_A2, subs_A3, 'Uniform', 0);
where subs_A1, subs_A2, subs_A3 are the cell arrays whose corresponding elements are to be multiplied.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!