Removing a row in a matrix using a variable

Let's say I have a (2x10) matrix.

Now I have a function to iteratively scroll through these rows, perform a cleaning process, and if some criteria is met, delete that row. So the function is returning (usually) a row of the original size, and sometimes removing that row. So, in other words, removal of a row is being done through a variable being returned by the function.

However, it throws an error regarding dimension mismatch.

So, I can do this:

x(1,:) = [];

To delete the first row, but:

d = [];
x(1,:) = d;

Gives a dimension mismatch error. So if I give:

x(1,:) = cleaning_function(x(1,:));

and the function decides to remove the row, it will throw up a dimension mismatch error.

How do I solve this issue?


The = [] is an idiom, and does not conform to the usual assignment rules (it is interpreted as "delete the LHS", more or less). That's why your idea isn't working. As Hoki says, the result you're aiming at would not be the most efficient. Still, you may have other concerns not revealed in your question. If so, the closest I can think of is to use a form as:

if cleaning_function(x(1, :))
    x(1, :) = [];

But Hoki's idea, or something similar, will be cleaner. For instance:

rows_to_keep = cleaning_function(x);
x = x(rows_to_keep, :);

Or, even:

x = cleaning_function(x)

with the details handled internally in a loop. Since Matlab passes by reference, the above is generally not an overhead.

Need Your Help

set timeout for socket receive

java sockets timeout udp

I want to send data to server, then wait for an answer for one minute and then close the socket.