Rajan Kasodariya
Rajan Kasodariya

Reputation: 27

How to delete a particular element from a matrix in SciLab

disp(A)

1.     4.     5.   
9.     13.    11.  
46.    29.    11.  

-->clear A(3,3) !--error 276 Missing operator, comma, or semicolon.

I want to delete 11

Upvotes: 0

Views: 4141

Answers (2)

Attila
Attila

Reputation: 615

In a regular matrix you can not delete just one element, as stated in the above answer: you have to delete a full row or a full column. However in a sparse matrix you can. A sparse matrix stores only the non-zero elements of the matrix. So if your data doesn't contain valid elements with 0 value, you can "delete" any element by setting it to 0 and then converting the matrix into sparse form with the sparse command:

A=rand(3,3);
disp(A,"original matrix:");
A(3,3)=0;
disp(A,"element is nulled out:");
A=sparse(A);
disp(A,"sparse matrix:");

This way you doesn't store the 0 values of tha matrix which may save memory or storage space. If you want to convert back, you can use the full command: the "missing" elements are representeted with zeros again:

B=full(A);
disp(B,"full form:");

But I think, that for missing or invalid values it's better to use %nan as advised above too: it's easier to deal with, more consistent, and you can have zeros in the matrix as valid data.

Upvotes: 0

spoorcc
spoorcc

Reputation: 2955

The clear command is used to remove entire variables, see the documentation. If you could clear the 3,3 field it would result in a malformed matrix:

1.     4.     5.  
9.     13.    11.  
46.    29.    

I don't know of a way this is possible.

What is it that you want? Do you want to set certain fields to be ignored. You could just set it to NaN (Not a Number) and check if it is nan later in your code with isnan:

A = rand(3,3)

A(3,3)= %nan

disp(A)
non_nans_indices = find(~isnan(A))

disp(A(non_nans_indices))

Upvotes: 1

Related Questions