how can i substitute elements of a symbolic matrix with arrays of numbers?

조회 수: 5 (최근 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
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
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
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 CenterFile Exchange에서 Sparse Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by