numbering of matrices generation

조회 수: 2 (최근 30일)
Ghulam Murtaza
Ghulam Murtaza 2016년 6월 23일
편집: Walter Roberson 2016년 6월 23일
hi if i have
for j=1:3;
B=[A(j,1); A(j,1)]
end;
then how i got the matrices B1 B2 B3 instead of only B .
  댓글 수: 1
KSSV
KSSV 2016년 6월 23일
How we would know how you got it without telling what is A and what you wanted?

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

답변 (1개)

Stephen23
Stephen23 2016년 6월 23일
편집: Stephen23 2016년 6월 23일
>> A = randi(9,3,1)
A =
6
5
9
>> B1 = A([1,1],1)
B1 =
6
6
>> B2 = A([2,2],1)
B2 =
5
5
>> B3 = A([3,3],1)
B3 =
9
9
But almost always creating numbered variables is a really bad idea. Just use indexing instead, because indexing is faster, more reliable, neater, and easier to read and understand than trying to create variable names dynamically. Read these to know why:
Or even better is to learn how to use MATLAB properly and avoid the loop altogether and use indexing:
>> A([1,1],1)
ans =
6
6
>> A([2,2],1)
ans =
5
5
Or if you really want to split A up into smaller matrices:
>> B = num2cell(A(:,[1,1]).',1);
>> B{1}
ans =
6
6
>> B{2}
ans =
5
5

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by