Solved

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

Posted on 2009-04-01
2
2,722 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
2 Comments
 
LVL 15

Accepted Solution

by:
yuk99 earned 450 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 50 total points
ID: 24067355
Yuk,

I tested your code.  Works fine.

Luke
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now