indices when the columns are all nan

조회 수: 1 (최근 30일)
Sagar
Sagar 2016년 3월 16일
댓글: Sagar 2016년 3월 17일
I am using [m, d] = nanmax (r_all); to get the maximum values and corresponding indices in matlab. There are several columns in r_all in which all the elements are nans. Unfortunately, nanmax returns '1' in indices for those columns that have all nans. However, there are actual indices that are value '1' so it creates confusion. How can I force matlab to return 'a' or 'b' as the output instead of returning 0' or '1'? Could you please suggest other solutions to this problem?
  댓글 수: 6
KSSV
KSSV 2016년 3월 16일
you attach your matrix.....
Sagar
Sagar 2016년 3월 16일
편집: Sagar 2016년 3월 16일
attached is the data. Please follow the link: https://drive.google.com/file/d/0B67659k3uth9Nks5OTUzZ2JRYU0/view?usp=sharing

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

채택된 답변

Stephen23
Stephen23 2016년 3월 16일
편집: Stephen23 2016년 3월 16일
There is no need to waste time using slow and ugly loops and if statements. You can locate those "incorrect" indices very simply using basic MATLAB indexing:
>> mat = [0,1,2,NaN,3,NaN;4,NaN,5,NaN,6,NaN;7,8,NaN,NaN,NaN,9]
mat =
0 1 2 NaN 3 NaN
4 NaN 5 NaN 6 NaN
7 8 NaN NaN NaN 9
>> [m,idx] = nanmax(mat) % max value and its indices
m =
7 8 5 NaN 6 9
idx =
3 3 2 1 2 3
>> idx(all(isnan(mat),1)) = NaN % replace all-NaN indices with NaN
idx =
3 3 2 NaN 2 3
  댓글 수: 3
Sagar
Sagar 2016년 3월 17일
편집: Sagar 2016년 3월 17일
Great, I like Guillaume's answer. :)
Sagar
Sagar 2016년 3월 17일
Thanks Stephen, apparently I didn't know there is 'all' function which I needed. :)

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

추가 답변 (1개)

KSSV
KSSV 2016년 3월 16일
clc; clear all ;
load data.mat ;
[m,n] = size(data) ;
idx = zeros(n,1) ;
val = idx ;
% column wise
for c = 1:n
[i,j] = nanmax(data(:,c)) ;
if isnan(i)
j = 50 ;
end
idx(c) = j ;
val(c) = i ;
end

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by