216 views (last 30 days)

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.

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.

Al Dente
on 27 Aug 2015

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

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

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
on 28 Aug 2015

Ugh, the awful eval strikes again. Read my comment above to know why this is a bad idea.

Opportunities for recent engineering grads.

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

Start Hunting!
## 1 Comment

## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/236999-how-do-i-assign-a-column-of-a-matrix-as-another-matrix#comment_306851

⋮## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/236999-how-do-i-assign-a-column-of-a-matrix-as-another-matrix#comment_306851

Sign in to comment.