MATLAB Answers

How do I assign a column of a matrix as another matrix?

216 views (last 30 days)
Samuel Davies
Samuel Davies on 27 Aug 2015
Edited: Stephen Cobeldick on 19 Jun 2019
Hi, I want to assign columns from a matrix A(row,col), with an arbitrary number of rows and cols, as matrices themselves. I assign a cutoff value, x, with y > x = 0 and then I want to be discarding the zero elements such that the matrices shorten each time keeping only nonzero elements. It looks something like this:
[x x x x [x [x [x
x x x x x x x
x x x 0 ---> x x x] etc
x x 0 0 x x]
x 0 0 0 x]
0 0 0 0] etc
Is it possible to have the labeling scheme something like below where it will auto assign the string name, so that the matrices become A1, A2, A3 etc..., I'm not sure how to do this:
for k = 1:1:cols
A'k'(:,k) = A(A>0)(:,k)
end
Thank you.

Answers (3)

Star Strider
Star Strider on 27 Aug 2015
Edited: Star Strider on 27 Aug 2015
There doesn’t seem to be an efficient, non-loop way to do this, so use a loop and a cell array to accommodate the different length vectors:
x = rand(5);
x = fliplr(triu(x)); % Create Matrix
for k1 = 1:size(x,2)
xc{k1} = x(x(:,k1)>0,k1); % ‘xc’ = Row Elements Of ‘x’>0
end
Here, ‘xc’ (‘x cell’) is the output.

  2 Comments

Al Dente
Al Dente on 27 Aug 2015
you're looping over the rows and not the columns, if this was not a square matrix you would get an error if the number of rows is more than the number of columns

Sign in to comment.


Guillaume
Guillaume on 28 Aug 2015
Edited: Guillaume on 28 Aug 2015
The proper way to do this is to store these new matrices into a cell array (certainly not to create numbered variable names using eval)
A simple one-liner does it:
z = [1 2 3; 4 5 6; 5 6 0; 7 0 0; 0 0 0]; %demo data
zcols = cellfun(@nonzeros, num2cell(z, 1), 'UniformOutput', false)
edit: typo pointed out by Star

  1 Comment

Star Strider
Star Strider on 28 Aug 2015
I forgot about the nonzeros function. (Note the ‘s’.)

Sign in to comment.


Al Dente
Al Dente on 27 Aug 2015
if z was matrix that looks like this:
z = [1 2 3; 4 5 6; 5 6 0; 7 0 0; 0 0 0]
I could get all the columns (positive numbers only) in seperate arrays like this (can be better but if I'm in a hurry I wouldn't think too much about it):
% note that this is a terrible thing to do if you're creating a function -- I wouldn't use eval ever like this
for m = 1:size(z, 2)
h = num2str(m); % m as a string
eval(['col_' h '=z(z(:,' h ')>0, ' h ')'])
end
%this code will result in:
%col_1 =
%
% 1
% 4
% 5
% 7
%
%
%col_2 =
%
% 2
% 5
% 6
%
%
%col_3 =
%
% 3
% 6
if I want a better way to do it, I would go with a cell array of arrays:
for m = 1:size(z, 2)
mycellarray{m} = z(z(:, m)>0, m);
end

  3 Comments

Samuel Davies
Samuel Davies on 28 Aug 2015
Hey Amr,
Good answer, it works for what I need. After that though is there a way to assign the output matrices such that something like this:
for i = 1:1:size(mycellarray, 2)
B'i' = cell2mat(mycellarray(:,i);
end
and to just have them all populate as variables in the workspace.
Thanks.
Guillaume
Guillaume on 28 Aug 2015
This is not a good answer! This is actually poor practice, popping up new variables using eval, which is extremely discouraged. A simple search on this forum will give plenty of reasons why.
There are much cleaner options to achieve the same thing.
Stephen Cobeldick
Stephen Cobeldick on 28 Aug 2015
Ugh, the awful eval strikes again. Read my comment above to know why this is a bad idea.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!