How do I assign a column of a matrix as another matrix?
12 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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.
Risposte (3)
Guillaume
il 28 Ago 2015
Modificato: Guillaume
il 28 Ago 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 Commento
Star Strider
il 27 Ago 2015
Modificato: Star Strider
il 27 Ago 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 Commenti
Al Dente
il 27 Ago 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
Al Dente
il 27 Ago 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
2 Commenti
Guillaume
il 28 Ago 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.
Vedere anche
Categorie
Scopri di più su Characters and Strings in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!