Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
to eliminate zeros

Subject: to eliminate zeros

From: kiruthiga

Date: 25 Mar, 2011 09:55:23

Message: 1 of 21

hi all

i want to eliminate the zeros in a matrix. here the each row and column lengths will change according to the number of zeros present in it. how to do this...


regards
kirthi

Subject: to eliminate zeros

From: John D'Errico

Date: 25 Mar, 2011 10:03:04

Message: 2 of 21

"kiruthiga" wrote in message <imhoqb$slc$1@fred.mathworks.com>...
> hi all
>
> i want to eliminate the zeros in a matrix. here the each row and column lengths will change according to the number of zeros present in it. how to do this...
>

MATLAB does not allow matrices with variable length rows
and/or columns. Certainly not so with BOTH rows and
columns varying.

Perhaps a sparse matrix will help you, if the goal is only
that of memory usage.

John

Subject: to eliminate zeros

From: ImageAnalyst

Date: 25 Mar, 2011 10:50:56

Message: 3 of 21

Or convert it to a cell array, where each cell contains a row vector
of possibly different lengths.

If you want to delete an ENTIRE row, then you can do this
M(rowToDelete, :) = [];

If you want to delete an entire column, then you can do this
M(:, columnToDelete) = [];

rowToDelete or columnToDelete can be a list of several column numbers
if you want. But deleting zeros randomly positioned around the array
will not allow you to delete entire rows or columns, unless the zeros
occur only in the entire row or column.

Subject: to eliminate zeros

From: kiruthiga

Date: 25 Mar, 2011 12:35:08

Message: 4 of 21

hi
thanks for your ideas pa. but my problem is different. i have

a= 0 -5 -7 -4 0
      0 0 0 0 0
      -5 0 0 0 0
      0 -2 0 -6 -1
     -3 -4 0 0 -8
      0 0 -1 0 0


in this i have to eliminate all '0's such tat


a= -5 -5 -7 -4 -1
     -3 -2 -1 -6 -8
         -4


i know this is difficult ... atleast i want the 1st row answer here...

a= -5 -5 -7 -4 -1 for further processing ... remaining values i dont need...
 

Subject: to eliminate zeros

From: Wayne King

Date: 25 Mar, 2011 12:59:04

Message: 5 of 21

"kiruthiga" wrote in message <imi25s$2dk$1@fred.mathworks.com>...
> hi
> thanks for your ideas pa. but my problem is different. i have
>
> a= 0 -5 -7 -4 0
> 0 0 0 0 0
> -5 0 0 0 0
> 0 -2 0 -6 -1
> -3 -4 0 0 -8
> 0 0 -1 0 0
>
>
> in this i have to eliminate all '0's such tat
>
>
> a= -5 -5 -7 -4 -1
> -3 -2 -1 -6 -8
> -4
>
>
> i know this is difficult ... atleast i want the 1st row answer here...
>
> a= -5 -5 -7 -4 -1 for further processing ... remaining values i dont need...
>

Hi, How are you coming up with a first row of nonzero elements to be -5 -5 -7 -4 -1?

You can certainly start with your A:

A =[0 -5 -7 -4 0
     0 0 0 0 0
    -5 0 0 0 0
     0 -2 0 -6 -1
    -3 -4 0 0 -8
     0 0 -1 0 0];

and do
B = A(A~=0);
but that will give you a vector -5 -3 -5 -2, etc. It will take the nonzero elements columnwise out of A. Not quite sure how you're reshaping after that because
reshape(B(1:10),2,5)

does not quite do what you want for a "first row"

Wayne

Subject: to eliminate zeros

From: Nasser M. Abbasi

Date: 25 Mar, 2011 12:59:51

Message: 6 of 21

On 3/25/2011 5:35 AM, kiruthiga wrote:
> hi
> thanks for your ideas pa. but my problem is different. i have
>
> a= 0 -5 -7 -4 0
> 0 0 0 0 0
> -5 0 0 0 0
> 0 -2 0 -6 -1
> -3 -4 0 0 -8
> 0 0 -1 0 0
>
>
> in this i have to eliminate all '0's such tat
>
>
> a= -5 -5 -7 -4 -1
> -3 -2 -1 -6 -8
> -4
>
>
> i know this is difficult ... atleast i want the 1st row answer here...
>
> a= -5 -5 -7 -4 -1 for further processing ... remaining values i dont need...
>

If I understand you right, one way, but I think there is a
faster way, but I am too lazy now:

---------------------------------
EDU>> A= [ 0 -5 -7 -4 0;
         0 0 0 0 0;
         -5 0 0 0 0;
         0 -2 0 -6 -1;
        -3 -4 0 0 -8;
         0 0 -1 0 0];
  
B = A~=0;
d = arrayfun(@(c) find(B(:,c)==1,1,'first'),1:size(A,2));
negs = arrayfun(@(i) A(d(i),i),1:length(d))
------------------------------


negs =
     -5 -5 -7 -4 -1


--Nasser

Subject: to eliminate zeros

From: Matt J

Date: 25 Mar, 2011 13:07:05

Message: 7 of 21


>> nz=cellfun(@(c) nonzeros(c),num2cell(a,1),'uni',0 );
>> ans=cellfun(@(c) c(1), nz)

ans =

    -5 -5 -7 -4 -1

Subject: to eliminate zeros

From: Nasser M. Abbasi

Date: 25 Mar, 2011 13:06:59

Message: 8 of 21

On 3/25/2011 5:59 AM, Nasser M. Abbasi wrote:

>
> If I understand you right, one way, but I think there is a
> faster way, but I am too lazy now:
>
> ---------------------------------
> EDU>> A= [ 0 -5 -7 -4 0;
> 0 0 0 0 0;
> -5 0 0 0 0;
> 0 -2 0 -6 -1;
> -3 -4 0 0 -8;
> 0 0 -1 0 0];
>
> B = A~=0;
> d = arrayfun(@(c) find(B(:,c)==1,1,'first'),1:size(A,2));
> negs = arrayfun(@(i) A(d(i),i),1:length(d))
> ------------------------------
>
>
> negs =
> -5 -5 -7 -4 -1
>
>


To handle the case of a column in A which happened
to be all zeros, then use this code instead

-------------------------------------
A= [ 0 -5 -7 -4 0;
         0 0 0 0 0;
         -5 0 0 0 0;
         0 -2 0 -6 0;
        -3 -4 0 0 0;
         0 0 -1 0 0];
  
B=A~=0;
d=arrayfun(@(c) find(B(:,c)==1,1,'first'),1:size(A,2),'UniformOutput',false);
d=[d{:}];
negs=arrayfun(@(i) A(d(i),i),1:length(d))
--------------------------------------

negs =
     -5 -5 -7 -4


--Nasser

Subject: to eliminate zeros

From: Nasser M. Abbasi

Date: 25 Mar, 2011 13:10:56

Message: 9 of 21

On 3/25/2011 6:07 AM, Matt J wrote:
>
>>> nz=cellfun(@(c) nonzeros(c),num2cell(a,1),'uni',0 );
>>> ans=cellfun(@(c) c(1), nz)
>
> ans =
>
> -5 -5 -7 -4 -1

SOmething wrong with the above:

-------------------------------
A= [ 0 -5 -7 -4 0;
         0 0 0 0 0;
         -5 0 0 0 0;
         0 -2 0 -6 0;
        -3 -4 0 0 0;
         0 0 -1 0 0];
  
nz=cellfun(@(c) nonzeros(c),num2cell(a,1),'uni',0 );
ans=cellfun(@(c) c(1), nz)
-------------------------------

ans =
     -5 -5 -7 -4 -1

The answer should be -5 -5 -7 -4

--Nasser

Subject: to eliminate zeros

From: Nasser M. Abbasi

Date: 25 Mar, 2011 13:13:23

Message: 10 of 21

On 3/25/2011 6:10 AM, Nasser M. Abbasi wrote:
> On 3/25/2011 6:07 AM, Matt J wrote:
>>
>>>> nz=cellfun(@(c) nonzeros(c),num2cell(a,1),'uni',0 );
>>>> ans=cellfun(@(c) c(1), nz)
>>
>> ans =
>>
>> -5 -5 -7 -4 -1
>
> SOmething wrong with the above:
>
> -------------------------------
> A= [ 0 -5 -7 -4 0;
> 0 0 0 0 0;
> -5 0 0 0 0;
> 0 -2 0 -6 0;
> -3 -4 0 0 0;
> 0 0 -1 0 0];
>
> nz=cellfun(@(c) nonzeros(c),num2cell(a,1),'uni',0 );
> ans=cellfun(@(c) c(1), nz)
> -------------------------------
>
> ans =
> -5 -5 -7 -4 -1
>
> The answer should be -5 -5 -7 -4
>


SOrry, used 'A' not 'a', here it is again:

------------------------------------------

EDU>> clear all

a= [ 0 -5 -7 -4 0;
         0 0 0 0 0;
         -5 0 0 0 0;
         0 -2 0 -6 0;
        -3 -4 0 0 0;
         0 0 -1 0 0];
  
nz=cellfun(@(c) nonzeros(c),num2cell(a,1),'uni',0 );
ans=cellfun(@(c) c(1), nz)
??? Attempted to access c(1); index out of bounds because numel(c)=0.

Error in ==> @(c)c(1)

--------------------------------

--Nasser

Subject: to eliminate zeros

From: Matt J

Date: 25 Mar, 2011 13:22:04

Message: 11 of 21

"kiruthiga" wrote in message <imi25s$2dk$1@fred.mathworks.com>...
>
>
> in this i have to eliminate all '0's such tat
>
>
> a= -5 -5 -7 -4 -1
> -3 -2 -1 -6 -8
> -4
>
>
> i know this is difficult ... atleast i want the 1st row answer here...
>
> a= -5 -5 -7 -4 -1 for further processing ... remaining values i dont need...
==================================


The truth of the matter, though, is that a FOR LOOP is probably going to be the most efficient solution. It's been found that other, vectorized MATLAB solutions tend to force you to do a lot of unnecessary data copying:


result=zeros(1,size(a,2));
    
for jj=1:size(a,2)
       
  for ii=1:size(a,1)
     
        result(jj)=a(ii,jj);
        
        if result(jj), break; end
        
   end
    
end

Subject: to eliminate zeros

From: Steven_Lord

Date: 25 Mar, 2011 13:36:50

Message: 12 of 21



"kiruthiga " <kirthi.chandrasekar@gmail.com> wrote in message
news:imi25s$2dk$1@fred.mathworks.com...
> hi thanks for your ideas pa. but my problem is different. i have
>
> a= 0 -5 -7 -4 0
> 0 0 0 0 0 -5 0 0 0 0
> 0 -2 0 -6 -1
> -3 -4 0 0 -8
> 0 0 -1 0 0
>
>
> in this i have to eliminate all '0's such tat
>
>
> a= -5 -5 -7 -4 -1
> -3 -2 -1 -6 -8
> -4

The second column has a different number of elements than the other columns;
this is not allowed in arrays in MATLAB as John stated.

> i know this is difficult ... atleast i want the 1st row answer here...
>
> a= -5 -5 -7 -4 -1 for further processing ... remaining values i dont
> need...

So you just want the first nonzero element in each column? What do you
expect to be in the result if a column contains no nonzero elements?

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: to eliminate zeros

From: Nasser M. Abbasi

Date: 25 Mar, 2011 13:40:16

Message: 13 of 21



>
> So you just want the first nonzero element in each column? What do you
> expect to be in the result if a column contains no nonzero elements?
>

I think OP just wants the first non-zero elements in each column.

If a column has all zeros, then that column contributes nothing. It
gets 'skipped'

--Nasser

Subject: to eliminate zeros

From: Steven_Lord

Date: 25 Mar, 2011 14:37:05

Message: 14 of 21



"Nasser M. Abbasi" <nma@12000.org> wrote in message
news:imi606$vd0$2@speranza.aioe.org...
>
>
>>
>> So you just want the first nonzero element in each column? What do you
>> expect to be in the result if a column contains no nonzero elements?
>>
>
> I think OP just wants the first non-zero elements in each column.

So do I, but I'd like to hear it from the OP directly.

> If a column has all zeros, then that column contributes nothing. It
> gets 'skipped'

That's one possibility, but there are others and I'd like to know EXACTLY
which one the OP wants. Better to get a full specification of the problem
before spending a lot of time solving a different one.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: to eliminate zeros

From: Florin

Date: 25 Mar, 2011 15:36:05

Message: 15 of 21

"kiruthiga" wrote in message <imi25s$2dk$1@fred.mathworks.com>...
> hi
> thanks for your ideas pa. but my problem is different. i have
>
> a= 0 -5 -7 -4 0
> 0 0 0 0 0
> -5 0 0 0 0
> 0 -2 0 -6 -1
> -3 -4 0 0 -8
> 0 0 -1 0 0
>
>
> in this i have to eliminate all '0's such tat
>
>
> a= -5 -5 -7 -4 -1
> -3 -2 -1 -6 -8
> -4
>
>
> i know this is difficult ... atleast i want the 1st row answer here...
>
> a= -5 -5 -7 -4 -1 for further processing ... remaining values i dont need...
>


Hi,

For your first line, you might want to try :

a=zeros(size(A,2),1);
for i=1:size(A,2)
 temp=nonzeros(A(:,i));
 if(~isempty(temp))
  a(i)=temp(1);
 end;
end

You can get the same way the second, third and so on line by copying temp(2) and temp of (3) respectively.

Also you did not mention what you want in case of a column filled with zeros. In your example
"> a= -5 -5 -7 -4 -1
> -3 -2 -1 -6 -8
> -4
is just an empty space for a(3,1). In the code i suggested you will obtain a zero but you can change it by treating the else case of ~isempty.

Btw, what method did you try ?
 
Regards,
Florin
 

Subject: to eliminate zeros

From: kiruthiga

Date: 25 Mar, 2011 15:38:06

Message: 16 of 21

illa pa... i ve matrix in which in each column there are atleast 2 non zero element, so no column ll nullify

Subject: to eliminate zeros

From: dpb

Date: 25 Mar, 2011 17:33:41

Message: 17 of 21

On 3/25/2011 10:38 AM, kiruthiga wrote:
> illa pa... i ve matrix in which in each column there are atleast 2 non
> zero element, so no column ll nullify

Say _WHAT_????

--

Subject: to eliminate zeros

From: Steven_Lord

Date: 25 Mar, 2011 17:56:23

Message: 18 of 21



"dpb" <none@non.net> wrote in message news:imijlj$vlv$2@dont-email.me...
> On 3/25/2011 10:38 AM, kiruthiga wrote:
>> illa pa... i ve matrix in which in each column there are atleast 2 non
>> zero element, so no column ll nullify
>
> Say _WHAT_????

I asked the OP what they wanted the solution to be if one of the columns in
their matrix has no nonzero elements; they responded that for their
particular problem each column contains at least 2 nonzero elements so the
question is moot.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: to eliminate zeros

From: dpb

Date: 25 Mar, 2011 18:00:02

Message: 19 of 21

On 3/25/2011 12:56 PM, Steven_Lord wrote:
>
>
> "dpb" <none@non.net> wrote in message news:imijlj$vlv$2@dont-email.me...
>> On 3/25/2011 10:38 AM, kiruthiga wrote:
>>> illa pa... i ve matrix in which in each column there are atleast 2 non
>>> zero element, so no column ll nullify
>>
>> Say _WHAT_????
>
> I asked the OP what they wanted the solution to be if one of the columns
> in their matrix has no nonzero elements; they responded that for their
> particular problem each column contains at least 2 nonzero elements so
> the question is moot.

I knew your question, Steve; glad you could translate the response... :)

I guess w/ your response I can go back and make the correlation but it
was surely gibberish the first pass thru...

--

Subject: to eliminate zeros

From: Steven_Lord

Date: 25 Mar, 2011 18:06:04

Message: 20 of 21



"dpb" <none@non.net> wrote in message news:imil70$7is$1@dont-email.me...
> On 3/25/2011 12:56 PM, Steven_Lord wrote:
>>
>>
>> "dpb" <none@non.net> wrote in message news:imijlj$vlv$2@dont-email.me...
>>> On 3/25/2011 10:38 AM, kiruthiga wrote:
>>>> illa pa... i ve matrix in which in each column there are atleast 2 non
>>>> zero element, so no column ll nullify
>>>
>>> Say _WHAT_????
>>
>> I asked the OP what they wanted the solution to be if one of the columns
>> in their matrix has no nonzero elements; they responded that for their
>> particular problem each column contains at least 2 nonzero elements so
>> the question is moot.
>
> I knew your question, Steve; glad you could translate the response... :)

I have a significant amount of experience at translating between
newsgroupese and English from my time on CSSM and other newsgroups. :)
CSSM's generally pretty good compared to some other newsgroups I've read.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: to eliminate zeros

From: ImageAnalyst

Date: 25 Mar, 2011 21:48:08

Message: 21 of 21

I agree that that was quite an impressive display of translatorial
gymnastics on your part Steve. My first response was "Huh?" - like
dpb's. But I'd still like to learn what "illa pa..." means. I did
understand most of it, with the help of your translation, but that
part still has me stumped.

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us