MATLAB Answers

# Column multiplication in the same matrix

11 views (last 30 days)
Mahmoud Solomon on 29 Mar 2018
Commented: Star Strider on 3 Apr 2018
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
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Accepted Answer

Star Strider on 29 Mar 2018
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 CommentsShowHide 7 older comments
Star Strider on 3 Apr 2018
As always, my pleasure.

Sign in to comment.

### More Answers (2)

Elias Gule on 29 Mar 2018
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 CommentsShowHide 1 older comment
Elias Gule on 29 Mar 2018
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

Sign in to comment.

Elias Gule on 29 Mar 2018
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
##### 1 CommentShowHide None
Mahmoud Solomon on 29 Mar 2018
It also worked but the result is not in a matrix format. I will work that out. Anyway, thanks.

Sign in to comment.

### Community Treasure Hunt

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

Start Hunting!