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:
Access to float coordinates in a 3D matrix

Subject: Access to float coordinates in a 3D matrix

From: Javier

Date: 3 Dec, 2012 13:08:07

Message: 1 of 11

Hi, I have several 3D lines to analyze a structure. The problem is that these coordinates aren't integer, for example 1.9 and I have the error:
Subscript indices must either be real positive integers or logicals.

I don't want to ceil, floor or round, cause if I do this the lines are modified, how could I access to these coordinates without changing the line?

Thanks very much

Subject: Access to float coordinates in a 3D matrix

From: Nasser M. Abbasi

Date: 3 Dec, 2012 13:19:02

Message: 2 of 11

On 12/3/2012 7:08 AM, Javier wrote:
> Hi, I have several 3D lines to analyze a structure. The problem is
>that these coordinates aren't integer, for example 1.9 and I have the error:
> Subscript indices must either be real positive integers or logicals.
>
> I don't want to ceil, floor or round, cause if I do this the lines
>are modified, how could I access to these coordinates without changing the line?

you seem to be using the value itself as an index.
use the location of the value as index. Not the value itself.

--Nasser

Subject: Access to float coordinates in a 3D matrix

From: Javier

Date: 3 Dec, 2012 13:31:07

Message: 3 of 11

"Nasser M. Abbasi" wrote in message <k9i8s3$vp5$1@speranza.aioe.org>...
> On 12/3/2012 7:08 AM, Javier wrote:
> > Hi, I have several 3D lines to analyze a structure. The problem is
> >that these coordinates aren't integer, for example 1.9 and I have the error:
> > Subscript indices must either be real positive integers or logicals.
> >
> > I don't want to ceil, floor or round, cause if I do this the lines
> >are modified, how could I access to these coordinates without changing the line?
>
> you seem to be using the value itself as an index.
> use the location of the value as index. Not the value itself.
>
> --Nasser

Thanks,

I use
x = x0 + t*dx
y = y0 + t*dy
z = z0 + t*dz
with t: 0:0.1:1, and d the direction vector.
This is the value of the coordinate isn't it?

Subject: Access to float coordinates in a 3D matrix

From: Javier

Date: 3 Dec, 2012 13:36:08

Message: 4 of 11

"Javier " <javsanzperez@gmail.com> wrote in message <k9i9ir$nki$1@newscl01ah.mathworks.com>...
> "Nasser M. Abbasi" wrote in message <k9i8s3$vp5$1@speranza.aioe.org>...
> > On 12/3/2012 7:08 AM, Javier wrote:
> > > Hi, I have several 3D lines to analyze a structure. The problem is
> > >that these coordinates aren't integer, for example 1.9 and I have the error:
> > > Subscript indices must either be real positive integers or logicals.
> > >
> > > I don't want to ceil, floor or round, cause if I do this the lines
> > >are modified, how could I access to these coordinates without changing the line?
> >
> > you seem to be using the value itself as an index.
> > use the location of the value as index. Not the value itself.
> >
> > --Nasser
>
> Thanks,
>
> I use
> x = x0 + t*dx
> y = y0 + t*dy
> z = z0 + t*dz
> with t: 0:0.1:1, and d the direction vector.
> This is the index of the coordinate isn't it?

Subject: Access to float coordinates in a 3D matrix

From: Nasser M. Abbasi

Date: 3 Dec, 2012 13:47:59

Message: 5 of 11

On 12/3/2012 7:31 AM, Javier wrote:

>> you seem to be using the value itself as an index.
>> use the location of the value as index. Not the value itself.
>>

> I use
> x = x0 + t*dx
> y = y0 + t*dy
> z = z0 + t*dz
> with t: 0:0.1:1, and d the direction vector.
> This is the value of the coordinate isn't it?
>

when you say `This is the value of the coordinate isn't it`,
to which `This` are you refering to?

any way, when you write

t: 0:0.1:1;
x0=1; dx=0.1;
x = x0 + t*dx

then use x(1) or x(5) to access values in the
x vector. do not use x(0.01) or x(0.5).

You loop index will be something like

for i=1:length(x)
     x(i) = .....
end

--Nasser

Subject: Access to float coordinates in a 3D matrix

From: Javier

Date: 3 Dec, 2012 14:11:07

Message: 6 of 11

"Nasser M. Abbasi" wrote in message <k9iaid$518$1@speranza.aioe.org>...
> On 12/3/2012 7:31 AM, Javier wrote:
>
> >> you seem to be using the value itself as an index.
> >> use the location of the value as index. Not the value itself.
> >>
>
> > I use
> > x = x0 + t*dx
> > y = y0 + t*dy
> > z = z0 + t*dz
> > with t: 0:0.1:1, and d the direction vector.
> > This is the value of the coordinate isn't it?
> >
>
> when you say `This is the value of the coordinate isn't it`,
> to which `This` are you refering to?
>
> any way, when you write
>
> t: 0:0.1:1;
> x0=1; dx=0.1;
> x = x0 + t*dx
>
> then use x(1) or x(5) to access values in the
> x vector. do not use x(0.01) or x(0.5).
>
> You loop index will be something like
>
> for i=1:length(x)
> x(i) = .....
> end
>
> --Nasser
>
This are the first values for x: x(1),x(2) and x(3): 3,54551732809566 3,63234837121917 3,71917941434267 3.

They are floating points because I have a decimal resolution. If for example I want to acces Structures (x(1),y(1),z(1)), I can't.

Subject: Access to float coordinates in a 3D matrix

From: Nasser M. Abbasi

Date: 3 Dec, 2012 14:28:40

Message: 7 of 11

On 12/3/2012 8:11 AM, Javier wrote:

>>
> This are the first values for x: x(1),x(2) and x(3):
>3,54551732809566 3,63234837121917 3,71917941434267 3.
>
> They are floating points because I have a decimal resolution. If for example
>I want to acces Structures (x(1),y(1),z(1)), I can't.
>

what exactly is your x above? is it a matrix? struct array?
cell? you show values with a "," in it, so confusing to guess
what you have.

You have to start with describing the data stucture your data
is in. Be clear what is it. If it is a matrix of 3 columns,
each column for a coordinates, then writing

x(i,:) gives you the 3 coordinates in the i'th row as a vector.

--Nasser

Subject: Access to float coordinates in a 3D matrix

From: dpb

Date: 3 Dec, 2012 14:56:02

Message: 8 of 11

On 12/3/2012 8:11 AM, Javier wrote:
...

>> > I use
>> > x = x0 + t*dx
>> > y = y0 + t*dy
>> > z = z0 + t*dz
>> > with t: 0:0.1:1, and d the direction vector.
>> > This is the value of the coordinate isn't it?
>> >
...

> This are the first values for x: x(1),x(2) and x(3): 3,54551732809566
> 3,63234837121917 3,71917941434267 3.
> They are floating points because I have a decimal resolution. If for
> example I want to acces Structures (x(1),y(1),z(1)), I can't.

Well, no, you can't. At the command line...from your values

 >> t= 0:0.1:1;
 >> d0=3.54551732809566;
 >> dx=0.0868310431235;
 >> x=d0+t*dx
x =
   Columns 1 through 7
     3.5455 3.5542 3.5629 3.5716 3.5802 3.5889 3.5976
   Columns 8 through 11
     3.6063 3.6150 3.6237 3.6323
 >>

Note that x is a row vector of 10 elements. You can't have indices that
aren't integers--what would the 3.5542...th entry in x be? The question
makes no sense posed that way.

What, specifically, are you trying to do that you're trying to write
x(a_x_value) instead of x(an_index_value)?

If you're trying to locate where within the array a particular value is
located, you can use find() or logical addressing (subject to the
caveats that equality-testing for floating point is subject to precision
problems so that you may not find the result if do exact comparisons).

At the command line again...

 >> ix=find(t==0.3)
ix =
      []

Note above returned []...what's up w/ that? Let's see...

 >> t(4)-0.3
ans =
   5.5511e-017

Ah, note the small discrepancy between the value of approximately 0.3
that you obtained from the floating point construction used to build the
t vector and that which was converted to internal representation when
typed in '0.3' at the command line...this is normal and one has to be
sure to deal with it when using floating point.

So, let's try something else...

 >> ix=find(abs(t-0.3)<1E-6)
ix =
      4
 >> x(ix)
ans =
     3.5716
 >>

Hopefully that will clarify the problem you're having. The lesson to
learn is


A) There _can't_ be anything but an integer array index

B) Floating point comparisons aren't exact

Again, clarify what you're _really_ trying to do that causes you to
write what you've written...

--

Subject: Access to float coordinates in a 3D matrix

From: Javier

Date: 3 Dec, 2012 15:39:10

Message: 9 of 11

dpb <none@non.net> wrote in message <k9iei4$g15$1@speranza.aioe.org>...
> On 12/3/2012 8:11 AM, Javier wrote:
> ...
>
> >> > I use
> >> > x = x0 + t*dx
> >> > y = y0 + t*dy
> >> > z = z0 + t*dz
> >> > with t: 0:0.1:1, and d the direction vector.
> >> > This is the value of the coordinate isn't it?
> >> >
> ...
>
> > This are the first values for x: x(1),x(2) and x(3): 3,54551732809566
> > 3,63234837121917 3,71917941434267 3.
> > They are floating points because I have a decimal resolution. If for
> > example I want to acces Structures (x(1),y(1),z(1)), I can't.
>
> Well, no, you can't. At the command line...from your values
>
> >> t= 0:0.1:1;
> >> d0=3.54551732809566;
> >> dx=0.0868310431235;
> >> x=d0+t*dx
> x =
> Columns 1 through 7
> 3.5455 3.5542 3.5629 3.5716 3.5802 3.5889 3.5976
> Columns 8 through 11
> 3.6063 3.6150 3.6237 3.6323
> >>
>
> Note that x is a row vector of 10 elements. You can't have indices that
> aren't integers--what would the 3.5542...th entry in x be? The question
> makes no sense posed that way.
>
> What, specifically, are you trying to do that you're trying to write
> x(a_x_value) instead of x(an_index_value)?
>
> If you're trying to locate where within the array a particular value is
> located, you can use find() or logical addressing (subject to the
> caveats that equality-testing for floating point is subject to precision
> problems so that you may not find the result if do exact comparisons).
>
> At the command line again...
>
> >> ix=find(t==0.3)
> ix =
> []
>
> Note above returned []...what's up w/ that? Let's see...
>
> >> t(4)-0.3
> ans =
> 5.5511e-017
>
> Ah, note the small discrepancy between the value of approximately 0.3
> that you obtained from the floating point construction used to build the
> t vector and that which was converted to internal representation when
> typed in '0.3' at the command line...this is normal and one has to be
> sure to deal with it when using floating point.
>
> So, let's try something else...
>
> >> ix=find(abs(t-0.3)<1E-6)
> ix =
> 4
> >> x(ix)
> ans =
> 3.5716
> >>
>
> Hopefully that will clarify the problem you're having. The lesson to
> learn is
>
>
> A) There _can't_ be anything but an integer array index
>
> B) Floating point comparisons aren't exact
>
> Again, clarify what you're _really_ trying to do that causes you to
> write what you've written...
>
> --

Thank you both. I explain better my problem: using the equations for a 3D line, I obtain the vectors x, y and z of type double, which are the coordinates of the structure (Images, a 3D matrix) that I want to study.
I have horizontal, diagonal and vertical lines.
For example,
I need to evaluate the coordinate x(1)= 3.56 y(1) = 8.56 z(1)= 58.2 but really this isn't any voxel. How could I obtain the coordinates of the closest voxel, in order to evaluate
Images (x(1),y(1),z(1))?

Subject: Access to float coordinates in a 3D matrix

From: dpb

Date: 3 Dec, 2012 17:31:37

Message: 10 of 11

On 12/3/2012 9:39 AM, Javier wrote:
> dpb <none@non.net> wrote in message <k9iei4$g15$1@speranza.aioe.org>...
...

>> What, specifically, are you trying to do that you're trying to write
>> x(a_x_value) instead of x(an_index_value)?
>>
>> If you're trying to locate where within the array a particular value
>> is located, you can use find() or logical addressing (subject to the
>> caveats that equality-testing for floating point is subject to
>> precision problems so that you may not find the result if do exact
>> comparisons).
...

>
> Thank you both. I explain better my problem: using the equations for a
> 3D line, I obtain the vectors x, y and z of type double, which are the
> coordinates of the structure (Images, a 3D matrix) that I want to study.
> I have horizontal, diagonal and vertical lines.
> For example,
> I need to evaluate the coordinate x(1)= 3.56 y(1) = 8.56 z(1)= 58.2 but
> really this isn't any voxel. How could I obtain the coordinates of the
> closest voxel, in order to evaluate
> Images (x(1),y(1),z(1))?

OK, that's a "more better" explanation... :)

Use 'nearest neighbor' method and interpolation to find the closest
index to a value. For your previous 1D case of x...

 >> ix=interp1(x,[1:length(x)],3.56,'nearest')
ix =
      3
 >> x(ix)
ans =
     3.5629
 >>

doc interp3 % to do it in all dimensions simultaneously

--

Subject: Access to float coordinates in a 3D matrix

From: Steven_Lord

Date: 3 Dec, 2012 22:16:40

Message: 11 of 11



"Javier " <javsanzperez@gmail.com> wrote in message
news:k9i87n$inl$1@newscl01ah.mathworks.com...
> Hi, I have several 3D lines to analyze a structure. The problem is that
> these coordinates aren't integer, for example 1.9 and I have the error:
> Subscript indices must either be real positive integers or logicals.
>
> I don't want to ceil, floor or round, cause if I do this the lines are
> modified, how could I access to these coordinates without changing the
> line?

There's no such thing as the 1.9th element of a matrix in MATLAB. You will
need to either locate the element "closest to" your desired element (for
some definition of "closest to") or perhaps interpolate between the 1st
element and the 2nd element.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

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