Gmail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Using 2D index with 3D matrix
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  3 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Robert  
View profile  
 More options Feb 1 2008, 10:03 am
Newsgroups: comp.soft-sys.matlab
From: "Robert " <robert.clement.stops...@ed.ac.uk>
Date: Fri, 1 Feb 2008 15:03:02 +0000 (UTC)
Local: Fri, Feb 1 2008 10:03 am
Subject: Using 2D index with 3D matrix
Hi

I am trying to insert a vector into a 3D matrix at specified
locations based on the values in a 2D matrix.  I am
currently  looping through the row column positions obtained
from the 2D matrix and reshaping the 1D matrix to assign it
to the 3D matrix.  

   My2D = round(10.*rand(10,10));
   My1D = rand(100,1);  
   My3D = zeros(10,10,100);

   [r c] = find(My2D == 5);
   d = length(My1D);

   for ix = 1:size(r)
      rx = r(ix);
      cx = c(ix);
      My3D(rx,cx,:)  = reshape(My1D,1,1,d);  
    end

This code works fine but I would like to know if there is a
way to speed up this assignment by using the single index
vector from the find statement to assign multiple 1D vectors
 to the 3D vector in a single step?

Thanks
Rob


    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Walter Roberson  
View profile  
 More options Feb 1 2008, 11:44 am
Newsgroups: comp.soft-sys.matlab
From: rober...@ibd.nrc-cnrc.gc.ca (Walter Roberson)
Date: Fri, 1 Feb 2008 16:44:29 +0000 (UTC)
Local: Fri, Feb 1 2008 11:44 am
Subject: Re: Using 2D index with 3D matrix
In article <fnvcb6$8g...@fred.mathworks.com>,

Robert  <robert.clement.stops...@ed.ac.uk> wrote:
>I am trying to insert a vector into a 3D matrix at specified
>locations based on the values in a 2D matrix.  I am
>currently  looping through the row column positions obtained
>from the 2D matrix and reshaping the 1D matrix to assign it
>to the 3D matrix.  
>   My2D = round(10.*rand(10,10));
>   My1D = rand(100,1);  
>   My3D = zeros(10,10,100);
>   [r c] = find(My2D == 5);
>   d = length(My1D);
>   for ix = 1:size(r)

Better (or at least clearer) would be size(r,1) instead of size(r)

>      rx = r(ix);
>      cx = c(ix);
>      My3D(rx,cx,:)  = reshape(My1D,1,1,d);  

You do not need the reshape there.

>    end
>This code works fine but I would like to know if there is a
>way to speed up this assignment by using the single index
>vector from the find statement to assign multiple 1D vectors
> to the 3D vector in a single step?

The target indices in My3D are:

bsxfun(@plus, ...
  (0:size(My1D,1)).*(size(My3D,1)*size(My3D,2)), ...
  sub2ind(size(My3D),r,c,repmat(1,10,1)))

to which you would assign

repmat(My1D,1,size(r,1)).'

You can do this in a single step without reshaping,

My3D(bsxfun(.....)) = repmat(....);
--
   "No one has the right to destroy another person's belief by
   demanding empirical evidence."                -- Ann Landers


    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ian Clarkson  
View profile  
 More options Feb 1 2008, 11:58 am
Newsgroups: comp.soft-sys.matlab
From: "Ian Clarkson" <ian.clark...@gesturetek.com>
Date: Fri, 1 Feb 2008 16:58:01 +0000 (UTC)
Local: Fri, Feb 1 2008 11:58 am
Subject: Re: Using 2D index with 3D matrix
"Robert " <robert.clement.stops...@ed.ac.uk> wrote in
message <fnvcb6$8g...@fred.mathworks.com>...

might be a bit quicker to do something like this:

My2D = round(10.*rand(10,10));
% change to a 3d array
My1D = rand([1 1 100]);  
My3D = zeros(10,10,100);

[r c] = find(My2D == 5);
My3D(r,c,:) = repmat(My1D,[length(r) length(c)]);

that does the assignment directly and doesn't use a for
loop.

hope it helps!


    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2010 Google