필터 지우기
필터 지우기

saving or not matrices

조회 수: 1 (최근 30일)
Nicolas
Nicolas 2011년 6월 21일
Hi,
I'm looking for a way of saving matrices depending on their content.
My loop is generating matrices that I want to save, however sometimes there is no proper values in it (only zeros that i replace by NaN).
I'd like to save only the matrices that have at least one proper value (> 0), but not the ones that have only zeros or NaN.
cheers
  댓글 수: 1
manoj saini
manoj saini 2011년 6월 21일
if a(:)~=0
%save matrix
end

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

채택된 답변

Matt Fig
Matt Fig 2011년 6월 21일
if ~all(isnan(A(:)))
% Do the saving.
end
But it seems to me that if the only source of nans is from when you replace the zeros with nans, then why not just skip this step and decide to save it based on whether the whole array is not equal to zero?
if any(A(:)) % Array with even one non-zero element passes.
% Do the saving.
end
  댓글 수: 1
Nicolas
Nicolas 2011년 6월 21일
oh yeah, that's better.. didn't know "any" though! thanks

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

추가 답변 (1개)

Nicolas
Nicolas 2011년 6월 21일
Actually I found that way
barycenter(barycenter(:,:)==0)=NaN; %replace zeros with NaN
if isnan(barycenter(1:1))<1
Names = sprintf('%s-%d', FileName, N(n));
xlswrite(Names, barycenter)
end
any advices?
  댓글 수: 2
Matt Fig
Matt Fig 2011년 6월 21일
Your method only checks if the first element is nan or not. It looks like you could learn to benefit from linear indexing. For example,
A(A(:)==0) = nan; % Index all elements at once with (:)
Nicolas
Nicolas 2011년 6월 21일
actually if at least a value appears in my matrix, then it has to be in the first cell, that's why I put ..(1,1). thanks for the tip though

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

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by