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!