Hello.
I have the following problem: I have a matrix A (3x3), and I want to add elements to this matrix and create table B with dimensions 6x6. More specifically, I want the new data to be the same as the original ones.
A=(1 2 3; 4 5 6; 7 8 9)
% A=(1 2 3
% 4 5 6
% 7 8 9)
B=(1 1 2 2 3 3; 4 4 5 5 6 6; 7 7 8 8 9 9)
% B=( 1 1 2 2 3 3
% 4 4 5 5 6 6
% 7 7 8 8 9 9)
That is, the first element of table A (1,1) is 1. Then, in positions (1,1) (1,2) of the new matrix (B) it must again be 1. In position A (1,2) it is 2, then in positions (1,3) and (1,4) of B to appear 2. Respectively with the other elements. How is this done? Your help is important.

 採用された回答

Ameer Hamza
Ameer Hamza 2020 年 12 月 30 日
編集済み: Ameer Hamza 2020 年 12 月 30 日

1 投票

Use repelem()
A = [1 2 3; 4 5 6; 7 8 9]
B = repelem(A, 1, 2)
Note: In MATLAB [ ] are used to create arrays.

6 件のコメント

stelios loizidis
stelios loizidis 2020 年 12 月 30 日
One more question. If based on the data in matrix A I want to make an matrix C (6X6) that has the following elements:
A=(1 2 3; 4 5 6; 7 8 9)
% A=(1 2 3
% 4 5 6
% 7 8 9)
C=(1 1 2 -2 3 -3; 4 -4 5 -5 6 -6; 7 -7 8 -8 9 -9)
% C=( 1 -1 2 -2 3 -3
% 4 -4 5 -5 6 -6
% 7 -7 8 -8 9 -9)
How is this done?
Ameer Hamza
Ameer Hamza 2020 年 12 月 30 日
Try this
A = [1 2 3; 4 5 6; 7 8 9]
B = repelem(A, 1, 2)
C = B.*repmat([1 -1], size(A))
Steven Lord
Steven Lord 2020 年 12 月 30 日
A = [1 2 3; 4 5 6; 7 8 9]
A = 3×3
1 2 3 4 5 6 7 8 9
B = kron(A, [1 -1])
B = 3×6
1 -1 2 -2 3 -3 4 -4 5 -5 6 -6 7 -7 8 -8 9 -9
stelios loizidis
stelios loizidis 2020 年 12 月 30 日
I tried the C = B.*repmat([1 -1], size(A)). But I have the following issue now. when I want to create the matrix D
% D=(1 1 -1 -1 2 2 -2 -2 3 3 -3 -3
% 4 4 -4 -4 5 5 -5 -5 6 6 -6 -6
% 7 7 -7 -7 8 8 -8 -8 9 9 -9 -9)
I use this: D = B.*repmat([1 -1], size(A)), but the following error occurs: "Matrix dimensions must agree".
Ameer Hamza
Ameer Hamza 2020 年 12 月 30 日
In this case, try
A = [1 2 3; 4 5 6; 7 8 9]
B = repelem(A, 1, 4)
D = B.*repmat([1 1 -1 -1], size(A))
or as Steven mentioned,
A = [1 2 3; 4 5 6; 7 8 9]
D = kron(A, [1 1 -1 -1])
stelios loizidis
stelios loizidis 2020 年 12 月 30 日
It works. Thank you very much!!!!

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および 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