Thread Subject: need fast partition method to go between 3d and 1d arrays

Subject: need fast partition method to go between 3d and 1d arrays

From: Yaman Aksu

Date: 11 Feb, 2007 01:11:09

Message: 1 of 2

Hello,

At the bottom is a past post that described a one-step use of ind2sub
to find indices of 3d array elements equal to a specified value. I'm
currently using it in a task which is

1. to create a collection of 1d arrays from a 3d array, one for each
distinct value of the (integer) 3d array.
2. to then reconstruct the 3d array from the 1d arrays
3. to do all of this *fast*, in the sense that I wish to avoid 'for'
loops and such and strictly only use operations on matrices

To achieve 3, I must change my current approach where I do the
following for each distinct value.
- use the shown use of ind2sub to get the 3 indices a,b,c associated
with that value
- in the 1d array associated with that value, set each array element
one at a time, with something like
for i=1:n
   array1d(i)=array3d(a,b,c)
end

How can I accomplish at least 1 and 3 together (putting aside 2 if we
must)? Thanks in advance for any help!

sureshanands@gmail.com wrote:

> I have a 3d numeric array and I need to get the subscript indices
> for all array elements equal to a specified value. The matlab
> 'find' doesn't seem to work for 3d arrays.

Bård Skaflestad wrote:

>Sure it does:
>
> [ix{1:ndims(A)}] = ind2sub(size(A), find(A(:) == value));
>
>By the way, using exact equality tests for floating point
>numbers may
>or may not be what you want.

--
Yaman Aksu
yamanaksu at yahoo dot com

Subject: need fast partition method to go between 3d and 1d arrays

From: Lars Gregersen

Date: 12 Feb, 2007 03:49:38

Message: 2 of 2

Yaman Aksu wrote:
> I'm
> currently using it in a task which is
>
> 1. to create a collection of 1d arrays from a 3d array, one for
> each
> distinct value of the (integer) 3d array.
> 2. to then reconstruct the 3d array from the 1d arrays
> 3. to do all of this *fast*, in the sense that I wish to avoid
> 'for'
> loops and such and strictly only use operations on matrices

I'm not sure that I fully understand your problem.

You can use 1D indices into a 3D matrix.

E.g.
A = floor(rand(3,4,5)*10);
f = find(A==0); % 1D index
B = zeros(size(A)); % 3D matrix
B(f) = 5; % use 1D index into a 3D matrix

If you are more comfortable using 1D arrays then you can put together
your array in 1D and tranform it into a 3D matrix:

A = floor(rand(3,4,5)*10);
f = find(A==0); % 1D index
B = zeros(prod(size(A))); % 1D array
B(f) = 5; use 1D index into a 1D array
C = reshape(B, size(A)); % convert into 3D matrix

Note that reshape'ing takes virtually no time.

  Lars

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

rssFeed for this Thread

Contact us at files@mathworks.com