A null assignment can have only one non-colon index
조회 수: 68 (최근 30일)
이전 댓글 표시
B=zeros(23,23)
for r=1:23;
for n=1:23;
if r==n
B(r,n)=A(r,n);
else
B(r,n)=[];
end
end
end
A is a symmetric 23*23 matrix and I want to remove all the other values except r=n. It gives me an "A null assignment can have only one non-colon index" error
댓글 수: 0
채택된 답변
KSSV
2018년 9월 11일
B=zeros(23,23)
for r=1:23;
for n=1:23;
if r==n
B(r,n)=A(r,n);
else
B(r,n)=NaN;
end
end
end
댓글 수: 6
Walter Roberson
2025년 9월 21일 20:19
B(r,n)=[]; attempts to delete a single element from the middle of an array. There is no way to leave a "hole" in the middle of an array, so either the request would have to be denied, or else the array would have to be reshaped as a single column with the one element left out...
like
Bnew = B(:);
Bnew(sub2ind(size(B),r,n)) = [];
B = Bnew;
MATLAB chooses to refuse the deletion request.
What is your expectation for B(r,n) = []; ? Are you expecting to get back an array with a hole in it? Are you expecting to get back an array that has the same leading rows as B before n but then has a "shorter" column n missing element #r, followed by trailing rows that are the full size? Are you expecting to get back a rectangular array that is missing the bottom right corner, with the other elements "flowing" into the empty space?
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!