フィルターのクリア

How to name every row of 100*2500 matrix?

2 ビュー (過去 30 日間)
Md Monirul Islam
Md Monirul Islam 2017 年 11 月 1 日
コメント済み: Steven Lord 2020 年 11 月 27 日
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 件のコメント
Md Monirul Islam
Md Monirul Islam 2017 年 11 月 1 日
編集済み: per isakson 2017 年 11 月 1 日
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,:).
Les Beckham
Les Beckham 2017 年 11 月 1 日
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.

サインインしてコメントする。

採用された回答

per isakson
per isakson 2017 年 11 月 1 日
編集済み: per isakson 2017 年 11 月 1 日
"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 件のコメント
Muhammad Ammar bin Faizul Azli
Muhammad Ammar bin Faizul Azli 2020 年 11 月 27 日
@Stephen Cobeldick
May i know what would your code be if i were to do the same for column?
Steven Lord
Steven Lord 2020 年 11 月 27 日
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

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGraph and Network Algorithms についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by