2 matrix combining first column and second

9 ビュー (過去 30 日間)
Eray Korkmaz
Eray Korkmaz 2020 年 10 月 2 日
回答済み: Mohammad Sami 2020 年 10 月 2 日
I need help about sth i have 2 matrices lets say
A=[1 B=[ 5
2 6
3] 7]
i need to create a third matrices which needs to be like this
C=[ 1 5
1 6
1 7
2 5
2 6
2 7... i need a code for this thanks.

回答 (2 件)

Ameer Hamza
Ameer Hamza 2020 年 10 月 2 日
If you have deep learning toolbox
A = [1 2 3];
B = [5 6 7];
M = fliplr(combvec(B, A).');
Result
>> M
M =
1 5
1 6
1 7
2 5
2 6
2 7
3 5
3 6
3 7
or without toolbox
A = [1 2 3];
B = [5 6 7];
[a, b] = meshgrid(A, B);
M = [a(:) b(:)];

Mohammad Sami
Mohammad Sami 2020 年 10 月 2 日
You can use the functions repmat and repelem.
if true
A = [1 2 3];
B = [5 6 7];
A = repelem(A,1,length(A));
B = repmat(B,1,length(B));
M = [A;B]';
end

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by