Creating a matrix having repeating elements

조회 수: 1 (최근 30일)
Rengin
Rengin 2019년 9월 4일
댓글: Rengin 2019년 9월 4일
% Dear users,
% Assume that I have a A matrix having the size of 5x5 and in the end;
% I want to get A=[1 -1 0 0 0; 1 -1 0 0 0; 0 1 -1 0 0; 0 1 -1 0 0; 0 0 1 -1 0]
% What is the easiest way to do it?
% Thanks in advance
  댓글 수: 6
Bruno Luong
Bruno Luong 2019년 9월 4일
OK here is different and still simple:
A=[1 -1 0 0 0; 1 -1 0 0 0; 0 1 -1 0 0; 0 1 -1 0 0; 0 0 1 -1 0]+0
The point is your question is not well formulated
Rengin
Rengin 2019년 9월 4일
Thanks but I was still be able to get my answer. It works perfectly fine.

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

채택된 답변

Andrei Bobrov
Andrei Bobrov 2019년 9월 4일
편집: Andrei Bobrov 2019년 9월 4일
A = kron(eye(5),[1;1])+kron(diag(-ones(4,1),1),[1;1]);
out = A(1:5,:);
or
n = 1:5;
A = repmat([1,-1,0,0,0],5,1);
out = A(mod(repmat(n,5,1) - ceil(n(:)/2),5)*5 + n(:));
  댓글 수: 2
madhan ravi
madhan ravi 2019년 9월 4일
You always impress without a doubt , a +1 from me.
Andrei Bobrov
Andrei Bobrov 2019년 9월 4일
Thank you Madhan!

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

추가 답변 (0개)

카테고리

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