A null assignment can have only one non-colon index
66 ビュー (過去 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!