Create a variable for each raw in a matrix

Dear all, Can you please help me to make variable for each raw in the following matrix?
%------------------------------------------------------------------
%clear all
bb= [487 498 225 225;77 480 241 241;79 94 246 246;475 98 249 249]
%cc=size(bb,1);
%
for k=1:size(bb,1)
v=bb(k,:)
end
%----------------------------------------
I need
v1= first raw
v2= second raw
v3= Third raw
v4= fourth raw
best regards

댓글 수: 8

per isakson
per isakson 2017년 6월 2일
편집: per isakson 2017년 6월 2일
Are &nbsp v1, v2, v3, v4 &nbsp intended to be four vectors?
Ali Noori
Ali Noori 2017년 6월 2일
No, its depends on the input matrix which may have different M*N
Ali Noori
Ali Noori 2017년 6월 2일
yes I need them as vectors
per isakson
per isakson 2017년 6월 2일
편집: per isakson 2017년 6월 2일
Why would you want to split &nbsp bb &nbsp into vectors? &nbsp Why not use &nbsp bb(1,:), ..., bb(end,:)&nbsp?
Ali Noori
Ali Noori 2017년 6월 2일
편집: per isakson 2017년 6월 3일
I can't, for example, if I used bb(5,:) (the fifth row) and the input matrix change to be 2*3 so I will have an error. therefore, I need a vector for each row in the changing matrix
Well, don't do that then. Don't access rows beyond size(bb, 1). Why would you even attempt this doomed thing - trying to get the 5th row of a matrix that's only 2 rows tall???
Anyway, it's a bad idea to do what you asked. See Walter's answer below, or the FAQ.
Stephen23
Stephen23 2017년 6월 2일
Do not do this. Although beginners sometimes imagine that magically creating variable names will solve all of their problems, in reality it just makes code buggy, slow, hard to debug, and hard to read. You might like to consider that the MATLAB documentation specifically advises against what you are trying to do: "A frequent use of the eval function is to create sets of variables such as A1, A2, ..., An, but this approach does not use the array processing power of MATLAB and is not recommended."
"Any alternative suggestions?"
Yes, use indexing. Indexing is simple, fast, efficient, neat, easy to read, makes debugging easy,...
You might also like to read what experienced MATLAB users say about this idea:
etc, etc
per isakson
per isakson 2017년 6월 3일
편집: per isakson 2017년 6월 3일
"I can't, for example, if I used bb(5,:) (the fifth row) ..." &nbsp is not a convincing answer. &nbsp If the size of the matrix isn't fixed you will always need to make checks. Example: you cannot assume that the variable, v5, exists; referencing v5 will sometimes throw an error.
Please read The XY Problem and try to explain your problem, X, to us. Everybody here seem to be convinced that creating &nbsp v1, ..., v5, &nbspsolving problem Y, will not truly help you.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

질문:

2017년 6월 2일

편집:

2017년 6월 3일

Community Treasure Hunt

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

Start Hunting!

Translated by