MATRICES with customized sum for rows and columns

4 ビュー (過去 30 日間)
MSH
MSH 2015 年 5 月 7 日
コメント済み: Stephen23 2015 年 5 月 21 日
I want to create 4 matrices (6x10) with sums of rows and columns as following; in a single program/code.
mat1: sum_row1=sum_row2=......=sum_row6=r(say) sum_col1=sum_col2=......=sum_col6=c(say)&&&&&&& r=c
mat2: sum_row1=sum_row2=......=sum_row sum_col1≠sum_col2≠......≠sum_col6
mat3: sum_row1≠sum_row2≠......≠sum_row6 sum_col1=sum_col2=......=sum_col6
mat4: sum_row1≠sum_row2≠......≠sum_row6 sum_col1≠sum_col2≠......≠sum_col6
Kindly suggest some logic/program
Thanks

採用された回答

Michael Haderlein
Michael Haderlein 2015 年 5 月 7 日
編集済み: Michael Haderlein 2015 年 5 月 7 日
To get mat1, you can use the magic function:
>> mat1=magic(4)
mat1 =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
>> sum(mat1,1)
ans =
34 34 34 34
>> sum(mat1,2)
ans =
34
34
34
34
mat2, mat3 and mat4 are easy, for instance use randoms and normalize the columns/rows:
>> matrand=rand(4);
>> mat2=bsxfun(@rdivide,matrand,sum(matrand));
>> sum(mat2)
ans =
1 1 1 1
>> mat3=bsxfun(@rdivide,matrand,sum(matrand,2));
>> sum(mat3,2)
ans =
1.0000
1.0000
1.0000
1.0000
mat4=matrand;
  3 件のコメント
Michael Haderlein
Michael Haderlein 2015 年 5 月 7 日
編集済み: Michael Haderlein 2015 年 5 月 7 日
Oh, sorry, I didn't read that you need 6x10 matrices. However I'm confident that in this case mat1 only exists if all the sums are zero. If this is fine with you, create the matrix with
>> matrand=randi(100,5,9);
>> mat1=[matrand -sum(matrand,2);-sum(matrand,1) sum(matrand(:))];
>> sum(mat1,1)
ans =
0 0 0 0 0 0 0 0 0 0
>> sum(mat1,2)
ans =
0
0
0
0
0
0
MSH
MSH 2015 年 5 月 21 日
THANKS

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by