how to Expanand any matrix

1 回表示 (過去 30 日間)
Nitin Sapre
Nitin Sapre 2019 年 9 月 1 日
コメント済み: the cyclist 2019 年 9 月 1 日
If Have 3x3 Matrix example
a = [-1 2 0;
2 1 -1;
1, 0, 1]
if my expansion factor is 5 i need to make eack element as 5x5 matrix with -1 replaced by all zero matris of 5x5,0 with identitiy matrix and 1 with identity right shift the column by one times and so on.
How can i do this?
  1 件のコメント
Andrei Bobrov
Andrei Bobrov 2019 年 9 月 1 日
編集済み: Andrei Bobrov 2019 年 9 月 1 日
Please show off your expanded matrix in your case for matrix a.

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

回答 (2 件)

the cyclist
the cyclist 2019 年 9 月 1 日
編集済み: the cyclist 2019 年 9 月 1 日
I am not certain I interpreted your question correctly, especially the "righth shift the column by ones times". But I believe that the algorithm below does what you want, or can be easily changed to do so.
a = [-1 2 0;
2 1 -1;
1, 0, 1];
expandFac = 5;
[m,n] = size(a);
A = zeros(expandFac*m,expandFac*n);
for mi = 1:m
for ni = 1:n
rowZero = expandFac*(mi - 1);
colZero = expandFac*(ni - 1);
switch a(mi,ni)
case -1
insertedMatrix = zeros(expandFac);
otherwise
insertedMatrix = diag(ones(1,expandFac-a(mi,ni)),a(mi,ni));
end
A(rowZero+1:rowZero+expandFac,colZero+1:colZero+expandFac) = insertedMatrix;
end
end
The key conceptual elements of this algorithm are
  • Preallocating the 15x15 array
  • For each element of a, finding the appropriating location to insert its corresponding 5x5 array. (That's what rowZero and colZero are doing)
  • For each element of a, figure what 5x5 to insert. (That's what the switch statement is doing.)
  • Use the diag command to create the identity and "shifted identity" arrays.

Andrei Bobrov
Andrei Bobrov 2019 年 9 月 1 日
編集済み: Andrei Bobrov 2019 年 9 月 1 日
out = cell2mat(arrayfun(@(x,y)diag(ones(5-x,1),x)*y ,abs(a),a>=0,'un',0));
or
z = cell(size(a));
k = zeros(5);
for ii = 1:numel(z)
if a(ii) >= 0
z{ii} = diag(ones(5-a(ii),1),a(ii));
else
z{ii} = k;
end
end
out = cell2mat(z);
  1 件のコメント
the cyclist
the cyclist 2019 年 9 月 1 日
I like how you handled the special case of a == -1 in your one-liner. I should have thought of that in my solution.

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

カテゴリ

Help Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

製品


リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by