Column multiplication in the same matrix

I have a 3 by 3 matrix and would like to loop and multiply column 1 with all other columns but not itself and also column 2 with other columns but not itself and column 3 with other columns but not itself.
x11 = [1 2 3; 4 5 3; 2 8 3];
[m12, n12] = size(x11);
for i = 1:m12
for j = 1:n12
x1x1(i, j)=x11(i, j).*x11(i, j)
end
end

 採用された回答

Star Strider
Star Strider 2018 年 3 月 29 日

0 投票

Try this:
x11 = [1 2 3; 4 5 3; 2 8 3];
[m12, n12] = size(x11);
for k1 = 1:n12
x11t = x11; % Copy ‘x11’
idx = setdiff(1:n12, k1) % Columns To Be Multiplied
x11t(:,idx) = bsxfun(@times, x11(:,idx), x11(:,k1)) % Assign Multiplied ‘Columns To Be Multiplied’ To Appropriate Columns Of ‘x11t’
x1x1(:,:,k1) = x11t; % Store In Output Array
end
It appears to do what you want.

8 件のコメント

Mahmoud Solomon
Mahmoud Solomon 2018 年 3 月 29 日
Thanks for the respond but I am not getting the desire result. The resulting matrix should look like this [2 6 3; 20 15 12; 16 24 6]. Again, column 1 multiplies columns 2 and 3 and column 3 multiplies column 1.
Star Strider
Star Strider 2018 年 3 月 29 日
Change the ‘x1x1’ assignment to:
x1x1(:,k1) = x11t(:,rem(2*n12+k1,n12)+1) % Store In Output Array
That puts the correct columns of ‘x11t’ into ‘x1x1’ in each iteration.
Mahmoud Solomon
Mahmoud Solomon 2018 年 3 月 29 日
Nice Sir. It works. Thanks again.
Star Strider
Star Strider 2018 年 3 月 30 日
As always, my pleasure.
Mahmoud Solomon
Mahmoud Solomon 2018 年 4 月 3 日
Sir... I need to generalize the solution to that problem. I have been trying but it's not working. What I want to achieve is when a column is used once, it should be removed from the original matrix thereby reducing the number of column by one. So, column 1 is used to multiple columns 2 to n. The next multiplication should be done between column 2 and 3 up to n, and in that order. Remember, nCr = n!/(n-r)!r!.
Star Strider
Star Strider 2018 年 4 月 3 日
It seems that my original solution (link) will do what you want.
If you begin with an (NxM) matrix, you would end up with a (NxMxM) 3D matrix, or perhaps (Nx(M-1)xM), depending upon what you want. I know of no other way to do that.
Mahmoud Solomon
Mahmoud Solomon 2018 年 4 月 3 日
Thanks again.
Star Strider
Star Strider 2018 年 4 月 3 日
As always, my pleasure.

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

その他の回答 (2 件)

Elias Gule
Elias Gule 2018 年 3 月 29 日

1 投票

Here you go:
[nr,nc] = size(x11);
for col = 1 : nc
col_data = x11(:,col);
col_prod = bsxfun(@times,col_data,x11(:,~all(ismember(x11,col_data))));
col_prod % you can store this in another array
end

2 件のコメント

Mahmoud Solomon
Mahmoud Solomon 2018 年 3 月 29 日
Thanks for the respond but I am not getting the desire result. The resulting matrix should look like this [2 6 3; 20 15 12; 16 24 6]. Again, column 1 multiplies columns 2 and 3 and column 3 multiplies column 1.
Elias Gule
Elias Gule 2018 年 3 月 29 日
ok. I see what you mean. You are basically multiplying the columns of a matrix while ignoring the currently selected column.
x11 = [1 2 3; 4 5 3; 2 8 3];
[nr,nc] = size(x11);
for col = 1 : nc
col_data = x11(:,col);
tempX11 = x11(:,~all(ismember(x11,col_data)));
col_prod = tempX11(:,1).*tempX11(:,2);
col_prod % you can store this in another array
end

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

Elias Gule
Elias Gule 2018 年 3 月 29 日

0 投票

I believe this works as expected:
x11 = [1 2 3; 4 5 3; 2 8 3];
[nr,nc] = size(x11);
for col = 1 : nc
col_data = x11(:,col);
tempX11 = x11(:,~all(ismember(x11,col_data)));
col_prod = tempX11(:,1).*tempX11(:,2);
col_prod % you can store this in another array
end

2 件のコメント

Mahmoud Solomon
Mahmoud Solomon 2018 年 3 月 29 日
It also worked but the result is not in a matrix format. I will work that out. Anyway, thanks.
Yuanli Guo
Yuanli Guo 2021 年 12 月 13 日
@Mahmoud Solomon Hi Mahmoud, do you know how to store these arrays in one matrix? I'm having the same problem now. Thank you^^

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

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by