How to subtract symbol associated matrix columnwise

1 回表示 (過去 30 日間)
Vincent Ike
Vincent Ike 2021 年 7 月 13 日
コメント済み: Vincent Ike 2021 年 7 月 14 日
A = [A; B; C; D]
B = [1 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16]
% my desired output is
C =
A-1 A-2 A-3 A-4
B-5 B-6 B-7 B-8
C-9 C-10 C-11 C-12
D-13 D-14 D-15 D-16
Thanks alot!

採用された回答

ANKUR KUMAR
ANKUR KUMAR 2021 年 7 月 13 日
編集済み: ANKUR KUMAR 2021 年 7 月 13 日
You cannot simply substract these two matrices, becasue B contains integers (double), and A has strings.
You need to convert B to strings, and then you can use cat function to get the desired output.
A = {'A','B', 'C', 'D'};
B = [1 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16];
B_str=arrayfun(@num2str,B,'un',0);
C=arrayfun(@(index) strcat(A{index},'-',B_str(index,:)), 1:size(B,2),'uni',0);
output_matrix=cat(1,C{:})
output_matrix = 4×4 cell array
{'A-1' } {'A-2' } {'A-3' } {'A-4' } {'B-5' } {'B-6' } {'B-7' } {'B-8' } {'C-9' } {'C-10'} {'C-11'} {'C-12'} {'D-13'} {'D-14'} {'D-15'} {'D-16'}
  1 件のコメント
Vincent Ike
Vincent Ike 2021 年 7 月 13 日
Thank you. You are very correct, although I was trying to relate the answer here to a question I asked ealier in https://www.mathworks.com/matlabcentral/answers/877563-solve-an-algebraic-matrix-equation. It is to solve a time dependent force function columnwise with A,B,C,D as the unknown variable, here converting the numeric to string may worsen my problem I think, cause I don't know much of MATLAB.

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

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2021 年 7 月 13 日
syms A B C D
A1 = [A; B; C; D]
A1 = 
B = [1 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16]
B = 4×4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
A1 - B
ans = 
  2 件のコメント
Vincent Ike
Vincent Ike 2021 年 7 月 14 日
You've been very helpful Walter. Many thanks!

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

カテゴリ

Help Center および File ExchangeSymbolic Math Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by