From: Matt J on
"Anthony Hopf" <anthony.hopf(a)gmail.com> wrote in message <htrlad$in3$1(a)fred.mathworks.com>...

> Matt, that is a very good point. but I use the index values to pick out points in a 3d xyz data structure, add them and replace the value in the 3d xyz matrix, these index values, as noted above, are not uniform from spot to spot (I am simulating a beam sampling space so the number of valid points increase with range from the sensor).
>
> I'll think about it, maybe I can apply this technique... Thanks again!
======

Well, do think about it, because I don't see why any of the above matters. For any vector of linear indices idx, there is an equivalent vector of logical indices idx_logical, which can be used to do all the same indexing operations with the exact same syntax.


From: Anthony Hopf on
"Matt J " <mattjacREMOVE(a)THISieee.spam> wrote in message <htrmbl$nn6$1(a)fred.mathworks.com>...
> "Anthony Hopf" <anthony.hopf(a)gmail.com> wrote in message <htrlad$in3$1(a)fred.mathworks.com>...
>
> > Matt, that is a very good point. but I use the index values to pick out points in a 3d xyz data structure, add them and replace the value in the 3d xyz matrix, these index values, as noted above, are not uniform from spot to spot (I am simulating a beam sampling space so the number of valid points increase with range from the sensor).
> >
> > I'll think about it, maybe I can apply this technique... Thanks again!
> ======
>
> Well, do think about it, because I don't see why any of the above matters. For any vector of linear indices idx, there is an equivalent vector of logical indices idx_logical, which can be used to do all the same indexing operations with the exact same syntax.
>
>

I have two questions concerning the above and something else that goes to the above

1. I am curious about the best way to build these sparse matrices. Right now I do an element by element comparison to find the values within a certain range:

for v=1:length(r_bin)
index_temp = find( r_3d >= rL(v) & r_3d <rH(v));
index_range(v,1:length(index_temp)) = index_temp;
clear index_temp;
end

The reason I am asking is because I don't really know how many points there will be in each vector index_temp... so I end up not being able to allocate all the space I would like in index_range... can I do this with a sparse matrix, or should I just do what I am doing here and make index_range a sparse matrix afterwords? (rL and rH are the upper and lower bounds of the range bin)

2. Given what you see here in number 1 I don't understand how I could create a logical array to do the same thing since each vector indexes points to different parts within r_3d and other matrices I'm not showing... Matt could you give me a clue?

Thanks again

Anthony
From: Matt J on
"Anthony Hopf" <anthony.hopf(a)gmail.com> wrote in message <hts2hl$3tk$1(a)fred.mathworks.com>...
> "Matt J " <mattjacREMOVE(a)THISieee.spam> wrote in message <htrmbl$nn6$1(a)fred.mathworks.com>...
> > "Anthony Hopf" <anthony.hopf(a)gmail.com> wrote in message <htrlad$in3$1(a)fred.mathworks.com>...
> >
> > > Matt, that is a very good point. but I use the index values to pick out points in a 3d xyz data structure, add them and replace the value in the 3d xyz matrix, these index values, as noted above, are not uniform from spot to spot (I am simulating a beam sampling space so the number of valid points increase with range from the sensor).
> > >
> > > I'll think about it, maybe I can apply this technique... Thanks again!
> > ======
> >
> > Well, do think about it, because I don't see why any of the above matters. For any vector of linear indices idx, there is an equivalent vector of logical indices idx_logical, which can be used to do all the same indexing operations with the exact same syntax.
> >
> >
>
> I have two questions concerning the above and something else that goes to the above
>
> 1. I am curious about the best way to build these sparse matrices. Right now I do an element by element comparison to find the values within a certain range:
>
> for v=1:length(r_bin)
> index_temp = find( r_3d >= rL(v) & r_3d <rH(v));
> index_range(v,1:length(index_temp)) = index_temp;
> clear index_temp;
> end
>
> The reason I am asking is because I don't really know how many points there will be in each vector index_temp... so I end up not being able to allocate all the space I would like in index_range... can I do this with a sparse matrix, or should I just do what I am doing here and make index_range a sparse matrix afterwords? (rL and rH are the upper and lower bounds of the range bin)
>
> 2. Given what you see here in number 1 I don't understand how I could create a logical array to do the same thing since each vector indexes points to different parts within r_3d and other matrices I'm not showing... Matt could you give me a clue?
================

Combining all the recommendations in this thread (e.g., loading the indices column-wise instead of row-wise), I get to the following:


%preallocate
N=length(r_bin);
I=cell(1,N);
J=I;


%compute sparse-indices
for v=1:N

I{v}=find( r_3d >= rL(v) & r_3d <rH(v) );

J{v}=I{v};
J{v}(:)=v;

end

index_range=sparse([I{:}], [J{:}],true); %final sparse logical matrix
From: Bruno Luong on
"Anthony Hopf" <anthony.hopf(a)gmail.com> wrote in message <hts2hl$3tk$1(a)fred.mathworks.com>...
>
>
> 1. I am curious about the best way to build these sparse matrices. Right now I do an element by element comparison to find the values within a certain range:
>
> for v=1:length(r_bin)
> index_temp = find( r_3d >= rL(v) & r_3d <rH(v));
> index_range(v,1:length(index_temp)) = index_temp;
> clear index_temp;
> end
>

You should definitively learn how to use function HISTC and family.

Bruno
From: Anthony Hopf on
"Bruno Luong" <b.luong(a)fogale.findmycountry> wrote in message <htt9rq$1s8$1(a)fred.mathworks.com>...
> "Anthony Hopf" <anthony.hopf(a)gmail.com> wrote in message <hts2hl$3tk$1(a)fred.mathworks.com>...
> >
> >
> > 1. I am curious about the best way to build these sparse matrices. Right now I do an element by element comparison to find the values within a certain range:
> >
> > for v=1:length(r_bin)
> > index_temp = find( r_3d >= rL(v) & r_3d <rH(v));
> > index_range(v,1:length(index_temp)) = index_temp;
> > clear index_temp;
> > end
> >
>
> You should definitively learn how to use function HISTC and family.
>
> Bruno

Bruno,

I had downloaded your histcn function at one point to try and understand how it could be applied to my problem, but I must not fully understand it's abilities. To me, I would think you would need to work from the edge of a matrix to use the HISTC and HISTCN functions. Starting from an arbitrary point (r=0 in the above case could be in the center of the matrix), I don't understand how the function could be used to bin evenly spaced cartesian 3d space into bins defined by their spherical transforms?

In the code above I am doing the first step of the indexing, indexing in range. r_3d is the range of (r,theta,phi) to each point within a 3d cartesian space referenced to some point within the 3d space. Can you work with me on how I would go about utilizing these functions to solve such a problem?

Thank you for the continued help, You guys are truly a essential piece to the matlab community!

Anthony