I need help with permutations.

for example: There are matrix m and r:
m = [1.2000 2.4000 1.3000
2.3000 1.5000 1.0000];
r = [0.2000 0.4000 0.3000];
rr = triu(ones(3),1);
rr(rr > 0) = r;
mm = bsxfun(@times,permute(m,[2,3,1]),permute(m,[3,2,1]));
K = bsxfun(@times,mm,rr + rr.' + eye(3));
This code gives me the result I want.
The values of the matrix m can vary. for example:
m = [1.2000 2.4000 1.3000
2.3000 1.5000 1.0000
1.2000 2.4000 3.0000];
r = [0.2000 0.4000 0.3000 0.4444];
So how can I write code like "[row col] = size (m)"?
I mean;
rr = triu(ones(row),1);
rr(rr > 0) = r;
mm = bsxfun(@times,permute(m,[?,?,?]),permute(m,[?,?,?]));
K = bsxfun(@times,mm,rr + rr.' + eye(row));

5 件のコメント

Adam
Adam 2017 年 5 月 5 日
What is your question?
Muhendisleksi
Muhendisleksi 2017 年 5 月 5 日
I want to write a more dynamic code because the dimensions of the matrix have changed.
Guillaume
Guillaume 2017 年 5 月 5 日
It's fairly clear that you aren't the author of the code you're showing (since it does not appear that you understand it). Can you provide a link to the question where you got this code so we have context.
Muhendisleksi
Muhendisleksi 2017 年 5 月 5 日
https://www.mathworks.com/matlabcentral/answers/338775-how-can-i-create-the-matrix-in-the-photo
Andrei Bobrov
Andrei Bobrov 2017 年 5 月 5 日
" m = [1.2000 2.4000 1.3000
2.3000 1.5000 1.0000
1.2000 2.4000 3.0000];
r = [0.2000 0.4000 0.3000 0.4444]; "
What result do you expect, what is the expression for K ?

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

回答 (1 件)

dpb
dpb 2017 年 5 月 5 日
編集済み: dpb 2017 年 5 月 5 日

0 投票

What's wrong with
[r,c]=size(m);
mm = bsxfun(@times,permute(m,[r,c,1]),permute(m,[c,r,1]));
ADDENDUM
Which, if you haven't recognized it, can be written as
permute(m,[size(m),1]),permute(m,[flip(size(m)),1])
ERRATUM
As Guillaume astutely points out, the permute indices are NOT size(m)-dependent, but reflect the dimensionality of m as 2D array. The above, while cute, is nonsensical for the purpose.

5 件のコメント

Muhendisleksi
Muhendisleksi 2017 年 5 月 5 日
I want to write a more dynamic code because the dimensions of the matrix have changed.
dpb
dpb 2017 年 5 月 5 日
Well, that's dynamic using the size() returned????
Muhendisleksi
Muhendisleksi 2017 年 5 月 5 日
My aim is to create these tables. However, as the values of "m" and "r" increase, the result will change.
Guillaume
Guillaume 2017 年 5 月 5 日
No idea what the original purpose of the code is, and I'm not going to try to decrypt some obscure image with no associated explanation, but it's clear to me that in the original code, the dimensions to permute do not depend on the size of m. It's always going to be [2 3 1] and [3 2 1] respectively.
The only thing that would need to change in the original code if the size of m changes is the size of rr, with
rr = triu(ones(size(m, 2)));
You would of course need more elements in r to accodomate the change in size of rr.
dpb
dpb 2017 年 5 月 5 日
Good catch, Guillaume! The permutations are not size-dependent but dimensional.
I didn't really try to read the image, either, and while the above will respond to OP's plea, it isn't useful. I'll scratch it altho I was kinda' proud of the flip(size()) thingie; I'd not thunk a' it before and can recall at least one instance could have used it to eliminate a temporary...

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

タグ

質問済み:

2017 年 5 月 5 日

コメント済み:

2017 年 5 月 5 日

Community Treasure Hunt

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

Start Hunting!

Translated by