Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Can't Index using NaN, but that not the problem

Subject: Can't Index using NaN, but that not the problem

From: Anthony Hopf

Date: 14 May, 2010 14:12:05

Message: 1 of 7

Hi everyone,

I have a little issue. Have a matrix of index values IND that I would like to use to index values in another matrix A... thing is IND has a bunch of NaN in it and I get a the error:
"??? Subscript indices must either be real positive integers or logicals."

when I do

TempA = A(IND);

This is due to the NaNs in IND. I found the following link which specifies a class that can be indexed my NaNs, but the problem is it will also return NaN values where NaN was used to index:

A=NanIndexable([10 20 30]);
IND = [2 nan 3];

>> A(IND) %The result you wanted
 
ans =
 
    20 NaN 30

I want it to return nothing where IND is NaN... any ideas?

reference to the link i found on matlab central
http://www.mathworks.se/matlabcentral/newsreader/view_thread/265702

Thank you,

Tony

Subject: Can't Index using NaN, but that not the problem

From: Steven Lord

Date: 14 May, 2010 14:23:00

Message: 2 of 7


"Anthony Hopf" <anthony.hopf@gmail.com> wrote in message
news:hsjlnl$o8g$1@fred.mathworks.com...
> Hi everyone,
>
> I have a little issue. Have a matrix of index values IND that I would
> like to use to index values in another matrix A... thing is IND has a
> bunch of NaN in it and I get a the error:
> "??? Subscript indices must either be real positive integers or logicals."

Then pull out the NaNs.

A(IND(~isnan(IND)))

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

Subject: Can't Index using NaN, but that not the problem

From: Jan Simon

Date: 14 May, 2010 14:27:23

Message: 3 of 7

Dear Anthony!

> I have a little issue. Have a matrix of index values IND that I would like to use to index values in another matrix A... thing is IND has a bunch of NaN in it and I get a the error:
> "??? Subscript indices must either be real positive integers or logicals."
>
> when I do
> TempA = A(IND);

TempA = A(IND(isfinite(IND))
or
TempA = A(IND(~isnan(IND))

Jan

Subject: Can't Index using NaN, but that not the problem

From: Anthony Hopf

Date: 14 May, 2010 18:05:22

Message: 4 of 7

"Jan Simon" <matlab.THIS_YEAR@nMINUSsimon.de> wrote in message <hsjmkb$pkq$1@fred.mathworks.com>...
> Dear Anthony!
>
> > I have a little issue. Have a matrix of index values IND that I would like to use to index values in another matrix A... thing is IND has a bunch of NaN in it and I get a the error:
> > "??? Subscript indices must either be real positive integers or logicals."
> >
> > when I do
> > TempA = A(IND);
>
> TempA = A(IND(isfinite(IND))
> or
> TempA = A(IND(~isnan(IND))
>
> Jan

Excellent... thank you Jan and Steve. This makes my find function look complicated but does the thing.

Subject: Can't Index using NaN, but that not the problem

From: Jan Simon

Date: 14 May, 2010 19:19:21

Message: 5 of 7

Dear Anthony!

> > TempA = A(IND(isfinite(IND))

> This makes my find function look complicated but does the thing.

Complicated?!
Let me remember Matt Fig's:
  regexprep(A,'([^/]+/)(?!/)','${strrep($1,$1,'''')}')
And Jason Breslau's equivalent:
  regexprep(A,{'.*[^/]/(?!/)', '\s'},{'', '_'})
And Nathan's:
  cellfun(@(x)strrep(x(max(regexp(x,'[^/]/[^/]'))+2:end),'','_'),A,'uni',false)
(all from the same thread here).

Have a nice day, Jan

Subject: Can't Index using NaN, but that not the problem

From: Anthony Hopf

Date: 14 May, 2010 19:46:04

Message: 6 of 7

"Jan Simon" <matlab.THIS_YEAR@nMINUSsimon.de> wrote in message <hsk7np$hf1$1@fred.mathworks.com>...
> Dear Anthony!
>
> > > TempA = A(IND(isfinite(IND))
>
> > This makes my find function look complicated but does the thing.
>
> Complicated?!
> Let me remember Matt Fig's:
> regexprep(A,'([^/]+/)(?!/)','${strrep($1,$1,'''')}')
> And Jason Breslau's equivalent:
> regexprep(A,{'.*[^/]/(?!/)', '\s'},{'', '_'})
> And Nathan's:
> cellfun(@(x)strrep(x(max(regexp(x,'[^/]/[^/]'))+2:end),'','_'),A,'uni',false)
> (all from the same thread here).
>
> Have a nice day, Jan

Haha, wow. touche. What does all of that do?

T

Subject: Can't Index using NaN, but that not the problem

From: Jan Simon

Date: 14 May, 2010 20:45:21

Message: 7 of 7

Dear Anthony!

> > regexprep(A,'([^/]+/)(?!/)','${strrep($1,$1,'''')}')
> > regexprep(A,{'.*[^/]/(?!/)', '\s'},{'', '_'})
> > cellfun(@(x)strrep(x(max(regexp(x,'[^/]/[^/]'))+2:end),'','_'),A,'uni',false)
>
> Haha, wow. touche. What does all of that do?

http://www.mathworks.com/matlabcentral/newsreader/view_thread/270630

Jan

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us