I have seen plenty of answers regarding how to remove leading and/or trailing zeros, and how to remove all zeros from a vector or matrix. What I need to do, though, is only remove some of them. I have two matrices, and I only want to remove the entries where both of them are zero. They are two-dimensional x and y coordinates, solved using characteristics (I can give more detail if needed) and I just want to remove the values where both matrices contain zeros at the same indices. I can easily convert the matrices into vectors and work with vectors, so any help in either case would be greatly appreciated.

For the sake of simplicity, let's assume you're using vectors called `X`

and `Y`

(of the same length), and you want to remove only those entries where both vectors are zero. Here's how (not tested):

```
% Find the indexes where either X or Y is different from zero
% (these are the indexes of the components we want to keep)
I = find(X~=0 | Y~=0);
% Select the desired components from X and Y
X=X(I);
Y=Y(I);
```

Edit: As Oli has pointed out below (and stefano explained further), you should use logical indexing for better performance.

Similar Questions

Given a vector of the counter-diagonals of a matrix in matlab, is there an easy way to reconstruct the matrix? For example, given x = [1 2 3 4 5 6 7 8 9] is there any easy way to reconstruct it to th

I'm trying to construct a vandermonde matrix using matlab. My input vector is t = [ 1 2 3 4 ]' My output using the vander function is: ans = 1 1 1 1 8 4 2 1 27 9 3 1 64 16 4 1 Now if I'm not mistak

What do you guys/gals think would be the best way to remove leading zeros from a QString? I am dealing with numbers like: 099900000002 008800000031 88800000043 Do I have to iterate over every charact

I have so far been using a = logical(zeros(10,10)) to create logical matrix. Is there a way to create it directly?

I made a simulation in Matlab that outputs a matrix. So after 500 runs I'll have 500 matrices. How can I choose a certain row from those matrices? The row is [xxx 1 0 0 0], where xxx means the existin

I have two matrices A & B in Matlab, for example A=[0,0,1,2,3,0,4,2,0] B=[2,3,1,2,2,3,4,4,1] What I want to do is to set elements in B to zero where they have the same position as zero elements

I'm trying to remove certain characters. At the moment I have output like cityname district but I want to remove cityname. SELECT Ort FROM dbo.tblOrtsteileGeo WHERE GKZ = '06440004' Output: Büdingen

I have a matrix x = [0 0 0 1 1 0 5 0 7 0] I need to remove all of the zeros such as x=[1 1 5 7] The matrices I am using are large (1x15000) I need to do this multiple times (5000+) Efficiency is key

I've a 256x256 projection matrix. each row is a projection taken with equal angles. i need to generate the original image with backprojection using matlab and I am not really familiar with matlab. Can

In each iteration of a loop, I am calculating a MATLAB matrix. These matrices all must be concatenated together to create one final matrix. I know the dimensions of this final matrix before entering t

I need to remove trailing zeros from BigDecimal along with RoundingMode.HALF_UP. For instance, Value Output 15.3456 <=> 15.35 15.999 <=> 16 //No trailing zeros. 15.99 <=> 15.99 15.00

For some gesture recognition software I want to build a recognition tree using the matlab function classregtree. When I have a binary image of only the gesture, I create 2 intensity spectra: one for

If I am using the sparse.lil_matrix format, how can I remove a column from the matrix easily and efficiently?

I have a matrix which shows me the posiions of a knight in a knights tour. I'm looking to find a way of firstly finding the numbers in order and outputting their positions e.g. on a smaller board X X=

I have an AxB matrix and two binary column vectors, Ax1 and Bx1 in Matlab. How can i select the rows of the matrix corresponding to 1 of Ax1 vector? Obviously, I have to do the same operation on the c

In the matrix 0 1 0 0 1 0 1 0 1 1 0 0 0 0 0 0 I'd like to reshape it by peeling off the 4th column and 4th row into 0 1 0 1 0 1 1 1 0 What is the smart way to go about doing this?

Is there a way to remove only the axis lines in the MATLAB figure, without affecting ticks and tick labels. I know that, box toggles the upper and right axes lines and ticks and that works perfect

I want to remove certain state/Province from magento 1.12.0.2 checkout (onestepcheckout) page like American Samoa I tried changing the Data.php file in /app/code/local/Mage/Directory/ public func

Does anyone know of a way in php to take a block of text and remove iframes if they aren't from a domain in a whitelist array or black list array? So i can allow iframes from like YouTube, Facebook, b

Is there a way to generate N x N random diagonalizable matrix in MATLAB? I tried as following: N = 10; A = diag(rand(N,N)) but it is giving me an N x 1 matrix. I also need the matrix to be symmetric.

How do I return only the rows of a matrix 'A' that not contain some values (These values are an array 'B')? A = {'A1', 5 'P01,P02,P03,P04,P07'; 'A2' 7, 'P07,P10'; 'A3' 8, 'P07,P09'; 'A4' 8, 'P10,P1

Well, maybe a odd question, but how would I handle unhandled exceptions only from certain assemblies in a application, but including exceptions that originate from .NET itself, like for instance when

In MATLAB I have a very large matrix (matrix A). Now I would like to find the row-index of the row which contain certain values in the second column. These values - which I'm looking for in Matrix A -

I have matrix a <500 x 500> and matrix b <500 x 2>. Matrix b contains two types of values which are row and column coordinates for matrix a. I would like to use the values in matrix b to t

I need help please, How do I return a matrix from a function in matlab? I have a matrix with zeros(size NxN). I send the matrix to some function to update her. How do I return the updated matrix? in a

I have a problem with removing surplus rows in the end of the matrix. In general, I need to remove rows that contain a specific elements in a specific column, without using a loop. It seems easy but I

How could I remove duplicates of characters found in array $a from string $b only if they are found next to each other? $a = array(a,b,c); $b = aabcccdef; Output should be abcdef Characters

Just like the title says, how do I tell the page to output the HTML code and remove certain characters, such as this character (ü)

Let's say I have a 10 x 10 matrix. What I do then is iterate through the whole matrix with an 3 x 3 matrix (except from the edges to make it easier), and from this 3 x 3 matrix I get the mean/average

What is the best way to efficiently remove columns from a sparse matrix that only contain zeros. I have a matrix which I have created and filled with data: matrix = sp.sparse.lil_matrix((100, 100)) I

Now first off, I am not even sure this is called a matrix, and I am new to MATLAB. But let's say I have a matrix that looks like this: for n=1:10 ... someImage = mat(:,:,n) %The matrix ... end wh

In Matlab I've got a big matrix (512x512x100). In order to analyze it I have used blockproc. Due to the fact that blockproc only accepts 2D matrices I have transformed my original matrix into a 2D mat

I want to remove everything from a string using regular expression except alpha and numeric characters and I need any leading zero removed. The below works but does not remove leading zeros $string =

This question already has an answer here: Faster way to initialize arrays via empty matrix multiplication? (Matlab) 4 answers /edit: See here for an interesting discussion of the topic. Thanks

In Matlab figure, I would like to remove ticks only from the top and right axes with keeping the plot box on. I know if I make the plot box off, the ticks on the top and right go away. But, this is no

I have a matrix 2x4, it looks like that m_E = 1.0e+30 * 8.161150222102073 Inf Inf 0.000000000000000 Inf Inf Inf 0.000000000000000 How I can display the matrix to make it looks without the first line

If I have a matrix X with only one column and it has some negative values in some rows. How can I remove only the negative values? Example: X=[-1; 2; 3; -4; 5] should become: X=[2; 3; 5] Also, how c

I have the following string values. 00000062178221 00000000054210 00004210555001 How can I cleanup the string and remove the zero paddings from the left? I'm using C# and .net 2.0. Expected results f

I have to create an n x n matrix with 199, 409, 619,....210n-11 down the diagonal and zeros everywhere else. Here is my M file so far: function A = MyDiagMatrix(n) A = zeros(n,n); for i =199:210:210n-

In matlab, I have a double datatype variable named Label with dimension 1211 x 1. I would like to create a IndicatorMatrix(6 columns) such that if a row in the Label variable is 34 then the correspond

Hey guys I am making a project on PhantomJS where I read source code of some webpages and i extract some info i need from there. I successfully finished the work just one issue I am facing right now.

Suppose I have a Matlab matrix A(x,y) which is square matrix with size N+1. I want a new matrix B, whose element B(x,z) is A(x,N-x-z), and in case x+z>N, B(x,z) is just zero. Of course I can do it

I have a 4d matrix (time-by-group-by-latitude-by-longitude) in MATLAB needs to be divided by a summed value from all groups in a 3d matrix (time-by-latitude-by-longitude) over a period of time. I woul

Delphi 7 How do i remove leading zeros in a delphi string? Example: 00000004357816 function removeLeadingZeros(ValueStr: String): String begin result:= end;

I have a string e.g. Dim str as string = xxxxxxxxxxxxxxxxxxxx£xxx£xxxx**£**xxxxxxxxxx I want to remove £ in the bold which is always at certain position (11th for instance) from the end. The whole

I have a plot in MATLAB from which I would like to remove the xticks but keep the xticklabels. If I just remove the xtick like so: set(gca, 'XTick', []); ...then the labels also disappear. Is there a

Consider a following branch setup: production branch qa branch staging branch several development branches Is there a way to only allow merging from a specific branch, for example allow the produ

I have the sparse Matrix having 300 to 900 rows with 3 columns, I want the sampling of this matrix i.e 20 samples of Matrix of the whole Matrix. How can I sample my matrix MAT in Matlab.

I have a list: my_list = ['0300', '0023', '0005', '000030'] I want to remove the preceding zeroes in each string element from the list. So I want strip the 0s from the left side of the string. Output

I have a matrix in matlab as follows: 1 1 1 2 2 1 3 3 0.075 12 3 0.025 4 4 1 5 5 1 6 6 1 I'm trying to find the value of the third column, given that the VALUE not the index of the first 2 columns ar