Storing the generated matrices from a for loop into cell arrays and access them later to multiply them altogether

1 回表示 (過去 30 日間)
I have to write the corresponding MATLAB algorithm to get the first column of M = (A - x_k*I)(A - x_(k-1)*I)(A - x_1*I), where A is an nxn matrix, I is the identity matrix, and x is a vector of length k.
Here is my algorithm:
function [M] = double1(A, x)
k = length(x);
[n,n] = size(A);
for i = k:-1:1
M{i} = A-x(i)*eye(n);
end
I want to store the matrices that I get from my for loop into a cell array and access them later and multiply them altogether. How to do that? Any help, please?

回答 (1 件)

Stephen23
Stephen23 2020 年 9 月 19 日
編集済み: Stephen23 2020 年 9 月 19 日
No cell array required:
>> x = [2,5,23];
>> A = [9,8;7,6];
>> [n,n] = size(A);
>> M = 1;
>> for k=numel(x):-1:1; M = M*(A-x(k)*eye(n)); end
>> M
M =
-728 -416
-364 -572

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by