In a 2D Matrix, How can we remove all zeros in row and column.

AFTpred(1,10);
PredAvail(10,10);
c=1;
for d=1:10
k=loc_of_r(1,d);
for i=1:10
if CommCost(k,i)~=0
AFTpred(1,i)= CommCost(k,i);
ss(c,i) = AFTpred(1,i);
sf(c,i) = AFTpred(1,i)+3;
PredAvail(c,i)=i;
end
end
c=c+1;
end
disp(PredAvail)
In this code i wanna clear all zeros from PredAvail matrix..Please help me out with this. Thanks in advanced..

4 Comments

You want to remove entire row and column which has even a single zero?
only zero i want to remove. All row will contains only non zero element.
You cannot remove zero, you have to replace that zero with some other number or nan. Is that okay?
I just wanna display all non zero element.

Sign in to comment.

Answers (1)

You have the following options:
clc; clear all ;
N = 5 ;
A = rand(5) ;
% intoroduce some zero rows/ columns
idx = randsample(1:N*N,5) ;
A(idx) = 0 ;
% replace zeros with nan's
B = A ;
B(A==0) = NaN ;
% Remove zeros
C = A ;
C(C==0) = [] ;

2 Comments

I want to display only non zero element of a 10*10 matrix namely PredAvail. How can i do this..Please help me out.
You got a code, make A as your matrix PredAvail.

Sign in to comment.

Asked:

on 22 Dec 2016

Commented:

on 22 Dec 2016

Community Treasure Hunt

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

Start Hunting!