A null assignment can have only one non-colon index

67 visualizaciones (últimos 30 días)
Chathurika Jayasundara
Chathurika Jayasundara el 11 de Sept. de 2018
Comentada: Walter Roberson el 21 de Sept. de 2025 a las 20:19
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

Respuesta aceptada

KSSV
KSSV el 11 de Sept. de 2018
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 comentarios
Amy
Amy el 21 de Sept. de 2025 a las 18:09
That's the same code that was giving them an error before tho?
Walter Roberson
Walter Roberson el 21 de Sept. de 2025 a las 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?

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by