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

1 view (last 30 days)
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
KSSV
KSSV on 22 Dec 2016
You cannot remove zero, you have to replace that zero with some other number or nan. Is that okay?

Sign in to comment.

Answers (1)

KSSV
KSSV on 22 Dec 2016
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
sophiya sheikh
sophiya sheikh on 22 Dec 2016
I want to display only non zero element of a 10*10 matrix namely PredAvail. How can i do this..Please help me out.

Sign in to comment.

Categories

Find more on Language Fundamentals in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!