Create Multiple Arrays While Looping Through One Single Array

I would like to loop through an array of 81 elements. For evey 9 iterations through the loop I would like to create a new array assigned to a different variable name. Basically, what I want is to loop through the array that has 81 elements for a set of 9 times that will create a new array of 9 elements. I would like to continue this for the entire 81 elements. So basically 1:9 of array 81 = 1st New Array, then 10:18 of array 81 = 2nd New Array and then 19:27 of array 81 = 3rd New Array and this pattern would continue until the array of 81 elements has been sorted into 9 array with 9 elements each.
Example PseudoCode
Array81 = [1 2 3 4 5 6 ................ 81]
For Loop:
Loop through array81 and create the following:
1stNewArray = [1 2 3 4 5 6 7 8 9]
2ndNewArray = [10 11 12 13 14 15 16 17 18]
3rdNewArray = [19 20 21 22 23 24 25 26 27]
4thNewArray = [28 29 30 31 32 33 34 35 36]
This will continue until the 9thNewArray = [73 74 75 76 77 78 79 80 81]

 Accepted Answer

By far the simplest and most efficient solution is to use one matrix:
V = 1:81
V = 1×81
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
M = reshape(V,9,9).'
M = 9×9
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
You can trivially access the rows of the matrix using very basic MATLAB indexing, e.g. the third row:
M(3,:)
ans = 1×9
19 20 21 22 23 24 25 26 27
This is simpler and much more efficient than the slow, inefficient, complex creation of lots of separate variables:
When you are starting to learn MATLAB is the right time to learn how to write good MATLAB code.

7 Comments

Thank you! I do like this option. I do have a follow up question if possible!
The reason why I wanted to loop through the array is I also need to create plots from the array. Essentially, I have multiple vectors that would need to be looped through and then certain amounts of each vector would need to output a corresponding plot.
Hopefully that was clear. I could post the example file that I am trying to work with to show what I am referring too. You can see the headers and corresponding columns in the file. But basically multiple arrays of V = 1:81 would be created such as V2 =1:81 and V3 = 1:81 and then each of those vectors would need to be created as how you mentioned above like M = (V, 9,9) M2 = V2(9,9,) and so forth and that would need to output plots using a plotting function such as plot(M(3,:), M2(3,:)).
Thank you for any help you can offer!
To store multiple matrices either use one 3D array or one cell array:
Either of those can be trivially accessed using indexing, which is simpler and much more efficient than anything involving dynamic named variables.
It is not clear to me how that file relates to your question. It can be imported using READTABLE:
T = readtable('Lab2_SatA940_0012.txt', 'VariableNamingRule','preserve')
T = 81×8 table
Wind Tunnel Setting (Hz) Air Temperature (degree C) Dynamic Pressure (Pa) Angle of Attack (degrees) Scannivalve Reading (in of H2O) Scannivalve Port # Velocity (m/s) Ambient Pressure (kPa) ________________________ __________________________ _____________________ _________________________ _______________________________ __________________ ______________ ______________________ 40 28.491 640.3 -12 2.389 1 33.915 96.5 40 28.508 641.12 -12 1.272 2 33.924 96.5 40 28.54 639.38 -12 0.619 3 33.891 96.5 40 28.533 640.91 -12 0.299 4 33.931 96.5 40 28.543 640.04 -12 0.153 5 33.933 96.5 40 28.547 640.11 -12 0.001 6 33.897 96.5 40 28.572 640.92 -12 -0.137 7 33.904 96.5 40 28.57 641.33 -12 -0.234 8 33.915 96.5 40 28.574 640.99 -12 -0.336 9 33.932 96.5 40 28.573 599.52 -10 3.068 1 32.796 96.5 40 28.584 598.67 -10 2.043 2 32.784 96.5 40 28.573 601 -10 1.302 3 32.844 96.5 40 28.578 598.41 -10 0.967 4 32.793 96.5 40 28.588 598.66 -10 0.787 5 32.806 96.5 40 28.604 602.03 -10 0.668 6 32.84 96.5 40 28.603 599.27 -10 0.575 7 32.758 96.5
Thanks Stephen. The file is the data that I need to work with. The V1 and V2, V3, so forth were the examples of accessing the matrix above. So V1 was Wind Tunnel Setting, V2 was Air Temperature, V3 Dynamic Pressure, so forth again.
I feel like this question may be complex but my ultimate goal is to get each vector in the matrix from the file and output 5 plots. My Y-axis for this plot is going to be the (Scannivalve data*249)/Dynamic Pressure data and I will assign that to variable Y. My X-axis will be a vector that is the following form X = [2 9 19 30 40 50 60 70 80]; I will have to manually create this vector/array.
my plot would have the following form
plot([0, X, 1], [1, Y(1:9), 0], 'b', [0, fliplr(X), 1], [1, fliplr(Y(73:81), 0], '--b');
The 0 and 1 in the brackets is my attempt at forcing conditions at the start and end of the plot for both the Y and X values. This will be for one plot. Then I will do it again but for a different plot and angle of attack.
hold on;
plot([0, X, 1], [1, Y(10:18), 0], 'b', [0, fliplr(X), 1], [1, fliplr(Y(64:72), 0], '--b');
I will need to do this 3 more times. The plot corresponds to each angle of attack which you will see there are a total of 10 different values but one is positive and one is negative which will need to go on the same graph. So -12 and 12 will both be on the same plot. -10 and 10 will be on another plot. A little background information is that the negative angle corresponds to the bottom surface of an airfoil and the positive angle corresponds to the top surface of the same airfoil. I then am plotting the pressure distribution across the bottom surface and top surface of the airfoil on one plot for each angle of attack. Hopefully that provides a little insight into the reason for questions.
So orginally, that is why I thought a for loop would be a good method of somehow iterating through those vectors/arrays and selecting 9 elements each time and formatting that to a plot for a total of 5 plots or 5 iterations if my thinking is correct. So basically a for loop within a for loop. I do like the indexing option and then maybe just manually creating the 5 plots individually but wasn't sure if there was a more concise way or writing out the code for something like this.
This should get you started (e.g. you will need to add your various data tweaks (some of which look a bit like an attempt to set the axes range, which is best done by actually setting the axes limits)).
Using dynamic variable names would make this task more complex. Indexing is simpler.
T = readtable('Lab2_SatA940_0012.txt', 'VariableNamingRule','preserve');
Y = 249 .* T.('Scannivalve Reading (in of H2O)') ./ T.('Dynamic Pressure (Pa)');
X = [2;9;19;30;40;50;60;70;80];
A = T.('Angle of Attack (degrees)');
U = unique(abs(A));
for k = 1:numel(U)
axh = subplot(2,3,k, 'NextPlot','add');
idx = +U(k)==A;
plot(X,Y(idx),'b','Parent',axh)
idx = -U(k)==A;
plot(X,Y(idx),'--b','Parent',axh)
title(axh,U(k))
end
Stephen! This is awesome! You are a lifesaver. I was able to implement everything including the forced conditions. The only thing that stumps me is that for 0 angle of attack I believe the pressure distribution should be just a straight line. You helped me out a ton so I will try to look into this further but I appreciate everything!
"The only thing that stumps me is that for 0 angle of attack I believe the pressure distribution should be just a straight line."
You can see that the Y-ranges are very different. You might like to set the Y-limits for all axes.
Oh good point, you are right I was able to set the limits and see the at 0° has pratically a 0 pressure distribution. Thank you again!

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Release

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!