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

조회 수: 11(최근 30일)
Joseph 2 Jan 2017
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?

#### 댓글 수: 1

Walter Roberson 2 Jan 2017
What size of output are you expecting?

로그인 to comment.

### 채택된 답변

Walter Roberson 2 Jan 2017
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

표시 이전 댓글 수: 2
Joseph 3 Jan 2017
just one more question, since now A is a cell array that contains 2*3 symbolic matrices, how to convert this to a numerical matrix in order to use it in a for loop? (i need to pick one 2*3 matrix and use it in an equation as a conversion matrix such c=b(2*3)*a in which a is initial value and c is final value. i need to do this until i get array of final values which is the same size as A). also i should say that i have 5 rrays like A that i need to multiply in order to get the final values. for better understanding of the process, i attached the same photo capture in this comment.i hope i could clear it up. thank you
Walter Roberson 3 Jan 2017
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 3 Jan 2017
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.

로그인 to comment.