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

采纳的回答

KSSV
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 个评论
Amy
Amy 2025-9-21,18:09
That's the same code that was giving them an error before tho?
Walter Roberson
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 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