Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How do I transform 1D array into 2D array without using for loops in Matlab?

Posted on 2009-04-01
2
Medium Priority
?
2,905 Views
Last Modified: 2016-02-10
Hi,

I've got a 1D array containing a large number of values, and I'd like to transform it to a 2D array where each sequence of 12 values is put in a row, so the size of the 2D array would be:
 number of rows = number of values/12
 number of columns = 12

I currently make this operation with a for loop, but it takes time for arrays containing large amount of data.

Someone would have an idea to optimize it?
for nrow=1:length(myarray)/12,
   for ncol=1:12,
      newarray(nrow,ncol)=myarray((nrow-1)*12+ncol);
   end
end

Open in new window

0
Comment
Question by:JulienVan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 15

Accepted Solution

by:
yuk99 earned 1800 total points
ID: 24039043
Use RESHAPE function. Don't have matlab right now to test by try this.

newarray=reshape(myarray,12,[])';

Open in new window

0
 
LVL 5

Assisted Solution

by:LukeyJay
LukeyJay earned 200 total points
ID: 24067355
Yuk,

I tested your code.  Works fine.

Luke
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
This video teaches viewers about errors in exception handling.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question