How to 'stretch' a matrix using a loop?

4 ビュー (過去 30 日間)
Roos de Bruijn
Roos de Bruijn 2017 年 6 月 11 日
編集済み: John BG 2017 年 6 月 12 日
Hi,
I have a 252x252 distance matrix. Now I need to reshape this matrix into a 3528x3528 matrix: so rows and columns times 14. I want the first 14 rows and columns, so a matrix of 1:14,1:14, filled with the first cell in the old matrix.
So Dnew 1:14,1:14 = Dold 1,1 Dnew 15:28,1:14 = Dold 2,1 D new 1:14,15:28 = Dold 1,2
How do I program this using a loop? Since I need to repeat it for every 1:14,1:14 matrix within my 3528x3528 matrix.
Thanks

採用された回答

Stephen23
Stephen23 2017 年 6 月 11 日
編集済み: Stephen23 2017 年 6 月 12 日
R2015a or later: use repelem:
repelem(M,14,14)
Earlier versions: use kron:
kron(M,ones(14))
  2 件のコメント
Roos de Bruijn
Roos de Bruijn 2017 年 6 月 11 日
Thanks, but how do I repeat this for 63504 elements?
When I fill out Dnew60 = repelem(distancematrix,[14,14])
it does not work.
Stephen23
Stephen23 2017 年 6 月 11 日
編集済み: Stephen23 2017 年 6 月 12 日
See my edited answer. In any case, this "works" for me:
>> M = rand(252,252);
>> N = kron(M,ones(14));
>> size(N)
ans =
3528 3528
And, according to the repelem documentation which I just read, this should work for a matrix M:
repelem(M,14,14)

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

その他の回答 (1 件)

John BG
John BG 2017 年 6 月 11 日
編集済み: John BG 2017 年 6 月 12 日
Hi Roos the Bruijin
the repelem syntax that you should be using is
repelem(Dold,2,14,14)
or simply
repelem(Dold,14,14)
the command repelem does not accept the syntax
repelem(Dold,[14 14])
suggested by Mr Cobeldick:
Another MATLAB improvement:
now one can directly run commands in the Internet browser, try it by opening this link
.
if you find this answer useful would you please be so kind to consider marking my answer as Accepted Answer?
To any other reader, if you find this answer useful please consider clicking on the thumbs-up vote link
thanks in advance
John BG
  2 件のコメント
Stephen23
Stephen23 2017 年 6 月 12 日
@John BG: thank you for pointing out the small error in my code, I have corrected it. On this forum it is certainly appreciated when others kindly point out simple and obvious syntax errors in a comment.
It is also appreciated when one takes the time to spell people's names correctly.
John BG
John BG 2017 年 6 月 12 日
thanks Mr Cobeldick, duly noted

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by