How do you add every row of a matrix to every other row to get separate new matix for each addition ?
2 ビュー (過去 30 日間)
古いコメントを表示
Avishka Karunaratne
2016 年 2 月 12 日
コメント済み: Hongyuan Wang
2018 年 11 月 23 日
I have a 10 by 10 matrix And if I were to consider each row as S1, S2 ..... S10 how would I be able to code in such a manner that I will be able to get S1+S1, S1+S2, ...S10+S10 ( as in every possible pair) to generate new matrices for each addition.
1 件のコメント
Matthew Eicholtz
2016 年 2 月 12 日
I do not understand what you want the output to be? For an N-by-N matrix, do you expect an N-by-N-by-N output array? Or N^2 1-by-N vectors?
採用された回答
Matthew Eicholtz
2016 年 2 月 12 日
I know this only partially answers your question, but it may help you get started.
x = magic(10); %sample 10x10 matrix
y = bsxfun(@plus,x,x(1,:)); %adds the first row of x to every row of x
0 件のコメント
その他の回答 (1 件)
Guillaume
2016 年 2 月 12 日
The cartesian product of two sets of number can be obtained easily with matlab with ndgrid. Possibly the easiest way to obtain what you want is to get the cartesian product of the row indices and pass that to arrayfun:
m = toeplitz(1:10, 1:2:19) %demo matrix
[ridx1, ridx2] = ndgrid(1:size(m, 1));
allsums = arrayfun(@(r1, r2) m(r1, :) + m(r2, :), ridx1, ridx2, 'UniformOutput', false)
Element (i,j) of the cell array corresponds to the sum of row i with row j. You can convert the cell array output to a 2D matrix, if you want:
vertcat(allsums{:})
参考
カテゴリ
Help Center および File Exchange で Matrices and Arrays についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!