How to Reshape a Data Array in MATLAB?

  • MATLAB
  • Thread starter Saladsamurai
  • Start date
  • Tags
    Matlab
In summary, the conversation was about a user trying to pick up data from a text file and organize it into an array with 5 consecutive elements in each row. The user was struggling with their current code and was suggested to use the "reshape" function in MATLAB, which would make the code more efficient. A sample code using "reshape" was provided.
  • #1
Saladsamurai
3,020
7
Ok. My brain is mush right now and I cannot seem to get this to work. I have a text file called 'xburned.dat' in the current directory that is a single column of data points that looks like this

Code:
9.91E-05
1.31E-04
1.38E-04
1.78E-04
2.31E-04
2.51E-04
2.81E-04
2.90E-04
3.01E-04
3.21E-04
3.41E-04
3.68E-04
4.57E-04
4.72E-04
5.01E-04
5.29E-04
5.56E-04
5.78E-04
5.85E-04
6.08E-04
6.21E-04
6.50E-04
6.68E-04
6.83E-04
7.10E-04
7.91E-04
8.30E-04
9.97E-04
1.13E-03
1.27E-03
.
.
.

I want to pick up the data in such a way that I have an array in which the rows are sets of 5 consecutive elements of xburned, i.e.:

Code:
9.91E-05 1.31E-04 1.38E-04 1.78E-04 2.31E-04
2.51E-04 2.81E-04 2.90E-04 3.01E-04 3.21E-04
.
.
.
I realize that there might not be a multiple of 5 of the data. In that case I can just truncate the data (i.e. just omit the last row that is < 5 points long).

This is my current code and it does not even come close ...

Code:
x_b = dlmread('xburned.dat');
len = length(x_b);
i = 1;
 while(i < len-5)
     k = 1;
     for j = 1:5
     points(k,j) = x_b(j,1);
     end
     i = i + 5;
     k = k + 1;
 end
 
Physics news on Phys.org
  • #2
Last edited by a moderator:
  • #3
MisterX said:
This could be done with http://www.mathworks.com/help/techdoc/ref/reshape.html". The following code is untested:

Code:
x_b = dlmread('xburned.dat');
n_rows = floor(size(x_b, 1)/5)); %number of rows in reshaped matrix
points = reshape(x_b(1:(n_rows*5)), n_rows, 5);

Thank you MisterX :smile: I will look into reshape. I got my code to work, but reshape looks much cleaner.
 
Last edited by a moderator:

Related to How to Reshape a Data Array in MATLAB?

1. What is MATLAB?

MATLAB is a high-level programming language and interactive environment commonly used by scientists and engineers for data analysis, visualization, and numerical computation.

2. How can I get help with MATLAB?

You can get help with MATLAB by using the "help" command in the command window, accessing the official MATLAB documentation, or seeking assistance from online forums and communities.

3. What are the main features of MATLAB?

MATLAB offers a wide range of features, including matrix manipulation, plotting and visualization tools, built-in functions for mathematical operations, and the ability to create and run scripts and functions.

4. Is MATLAB free to use?

No, MATLAB is not free to use. It is a commercial software with different pricing options depending on the intended use and the type of license.

5. Can MATLAB be used for any type of data analysis?

Yes, MATLAB is a versatile tool and can be used for various types of data analysis, including signal processing, image and video processing, machine learning, and more.

Back
Top