Create Multiple Arrays While Looping Through One Single Array

43 ビュー (過去 30 日間)
Zachary Giovanelli
Zachary Giovanelli 2021 年 9 月 19 日
コメント済み: Zachary Giovanelli 2021 年 9 月 21 日
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]

採用された回答

Stephen23
Stephen23 2021 年 9 月 20 日
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 件のコメント
Stephen23
Stephen23 2021 年 9 月 20 日
編集済み: Stephen23 2021 年 9 月 20 日
"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.
Zachary Giovanelli
Zachary Giovanelli 2021 年 9 月 21 日
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!

サインインしてコメントする。

その他の回答 (0 件)

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by