How to name every row of 100*2500 matrix?

currently i am working with a matrix. The dimension of the matrix is 100*2500. I need to assign each row the matrix to a new variable. How i can do this?

6 Commenti

David Goodmanson
David Goodmanson il 1 Nov 2017
Modificato: David Goodmanson il 4 Nov 2017
Hi Md,
The idea of a truckload of variable names, usually to label the individual rows or columns of a matrix, comes up frequently on this website. And every time someone weighs in to say that that style of coding is not a good idea. It's usually awkward, cluttered, susceptible to mistakes and, once created, resistant to improvements using the kind of array-oriented coding that Matlab is really all about. There are many advantages to using, e.g. A(32,:) for a row variable instead of making up a hard-coded variable like A32 in its place.
Dear per isakson , I need to do this because i want to apply DWT to every row of this matrix, i.e. i want to treat those row a 1D matrix and have 1D DWT applied to them using DWT GUI.
per isakson
per isakson il 1 Nov 2017
Modificato: per isakson il 1 Nov 2017
Ok, but
  • Have you considered to use a structure with 100 fields?
  • Those hundred names from where do they come? Do you have a cell array of good names? Do you want to create the names according to some rule?
  • Why doesn't [cA,cD] = dwt( M(rr,:), 'wname' ) where rr is row number work?
Let me clear it. Let data = ones(3,5); So all I want is as followings:
row1= data(1,:), row2=data(2,:) and row3= data(3,:).
But Matlab is designed to allow you to operate on matrices and (more specifically, in this case) subsets of matrices using indexing. That is what per has suggested and, I think, you should try. You don't need to give a separate name like row1 to data(1,:), etc. -- just apply your operation to data(1,:), probably using a loop where the 1 is replaced by the loop index. Then you don't have to code a hundred lines to process row1 through row100.
Please note especially the last bullet in per's last comment.

Accedi per commentare.

 Risposta accettata

per isakson
per isakson il 1 Nov 2017
Modificato: per isakson il 1 Nov 2017
"So all I want is as followings" See TUTORIAL: Why Variables Should Not Be Named Dynamically (eval). However, I guess you need to make your own mistakes to understand that it's a really bad idea.
M = ones( 3, 5 );
for rr = 1 : size(M,1)
variable_name = sprintf( 'row%03d', rr );
assign( variable_name, M(rr,:) );
end
where
function assign( varargin )
switch nargin
case { 2 },
if isvarname( varargin{ 1 } )
Name = varargin{ 1 };
else
error( ['poi: First input argument, ', ...
inputname(1), ' must be a legal name'] ),
end,
Value = varargin{ 2 };
otherwise
error( 'poi: Wrong number of input arguments' ),
end
assignin( 'caller', Name, Value );
end

6 Commenti

thanks a lot.
Stephen23
Stephen23 il 1 Nov 2017
Modificato: Stephen23 il 1 Nov 2017
@Md Monirul Islam: you might like to consider why so many experienced MATLAB users are telling to to avoid doing this. Perhaps they might have some good reasons for advising you to write better, neater, more reliable code?
You could also read the MATLAB documentation, which states that accessing variable names dynamically should be avoided:
One day when you actually want to write reliable, efficient, debuggable code then you can come back and we would be happy to show you how. It is actually simpler than what you are doing now!
Hello Stephen Cobeldick, I want to thank you for your wise advice. I understand your concern and others who have already warned me. As I have mentioned earlier, I want to label every row of given matrix. So now i have 1200 rows or 1200 new variables which i have saved a "mat file". now I need to 1D DWT on them using wavelet GUI. thanks again everyone.
Stephen23
Stephen23 il 1 Nov 2017
Modificato: Stephen23 il 1 Nov 2017
@Md Monirul Islam: if you had told us what you are actually trying to do, then we could have shown you better ways of doing it. For example, instead of wasting both your and MATLAB's time magically creating lots of separate variables just to save them in a .mat file, you can simply split the matrix rows into a structure, and then provide that structure to save:
>> M = rand(100,3);
>> C = sprintfc('row%d',1:size(M,1));
>> S = cell2struct(num2cell(M,2),C,1);
>> save('test.mat','-struct','S')
Did you see what I did there? Instead of using your complex and slow method using magical variable names, I used only simple, standard MATLAB concepts. My code uses just four lines, about one third the number of lines than your code does! It does not require any function to be defined. And is also easier to debug at the same time.
It is much better if you should tell us what your real task is, before deciding what slow, inefficient, and buggy code you will use:
@Stephen Cobeldick
May i know what would your code be if i were to do the same for column?
With functionality available now, I'd probably use a table array instead of a numeric array.
cities = ["Albuquerque"; "Boston"; "Chicago"; "Dallas"];
dist = array2table(magic(4), 'VariableNames', cities, 'RowNames', cities)
dist = 4x4 table
Albuquerque Boston Chicago Dallas ___________ ______ _______ ______ Albuquerque 16 2 3 13 Boston 5 11 10 8 Chicago 9 7 6 12 Dallas 4 14 15 1
distBC = dist{'Boston', 'Chicago'}
distBC = 10

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by