I want create a matrix such that
A matrix mat_B = [ 1 2 3 4 5; 6 7 8 9 10; 11 12 13 14 15; 16 17 18 19 20].
Is there any other code that more efficient than the code I make below?
for i = 1:5 if i ==1 a (i,:)=1:4; else a (i,:) = (a (i-1,:))+4; end end

 채택된 답변

Star Strider
Star Strider 2017년 6월 23일

0 개 추천

I would use the reshape function to avoid the explicit loops:
v = 1:20;
mat_B = reshape(v, 5, [])';

댓글 수: 2

balandong
balandong 2017년 6월 23일
Yes, I agree with you, this indeed a more compact solution. Thanks
Star Strider
Star Strider 2017년 6월 23일
My pleasure.

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

추가 답변 (0개)

카테고리

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

질문:

2017년 6월 23일

댓글:

2017년 6월 23일

Community Treasure Hunt

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

Start Hunting!

Translated by