• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3061
  • Last Modified:

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

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
JulienVan
Asked:
JulienVan
2 Solutions
 
yuk99Commented:
Use RESHAPE function. Don't have matlab right now to test by try this.

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

Open in new window

0
 
LukeyJayCommented:
Yuk,

I tested your code.  Works fine.

Luke
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now