How do i set empty matrix elements to zero?

조회 수: 16 (최근 30일)
joakim johansen
joakim johansen 2019년 3월 11일
댓글: Rik 2019년 6월 20일
How can i set the empty elements in the above picture to zero, i have tried T = cellfun('isempty', Z),
but it does not recognize the cells as empty.
Anyone got ideas of how to do this?
  댓글 수: 2
Guillaume
Guillaume 2019년 3월 11일
It's impossible for us to know from a screenshot what type of data you have. Assuming it's a cell array, it's also impossible for us to know if the cells are actually empty.
It's much better if you attach your data as a mat file. Screenshots are useless.
KALYAN ACHARJYA
KALYAN ACHARJYA 2019년 3월 11일
편집: KALYAN ACHARJYA 2019년 3월 11일
Please attach the file, so that we can try on it.

댓글을 달려면 로그인하십시오.

답변 (1개)

Navdha Agarwal
Navdha Agarwal 2019년 6월 20일
Following assumptions are made for the solution stated below:
  1. The csv file is being read as a matrix and some cells are actually empty.
  2. The name of the file is 'file1.csv'
  3. When the file is read as the matrix, the empty cells are automatically recognized as 'NaN'.
The code for the question, keeping the above assumptions is:
A = readmatrix('file1.csv');
dim = size(A);
for i = 1:dim(1)
for j = 1:dim(2)
if isnan(A(i,j))
A(i,j) = 0;
end
end
end
disp(A)
  댓글 수: 1
Rik
Rik 2019년 6월 20일
The loops are not needed, you can simply use the line below:
A(isnan(A))=0;

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by