How can I make a loop of the variables?

1 view (last 30 days)
Nut Pakwisutikul
Nut Pakwisutikul on 21 Jan 2016
Commented: Adam on 22 Jan 2016
Hi, Experts
In this particular case, there are 432 variables from X1 to X432, however there is an array inside each of the variable. The question is what will be a way to make a loop that sums up the array in each variable and continues to other variable. Thus, Having all the data in a table.
Thank you
% Fuselage Variables
CF = [100 0 8.50 0 0 3000 16 8 1.71 204.80 629.80];
AF = [120 40 6.30 24 36 3800 16 8 1.35 161.68 516.68];
BF = [135 60 7.00 36 48 3700 16 8 1.38 166.05 576.05];
% Engine Variables
CE = [130 0 6.50 0 0 450 8 4 2.84 477.87 802.87];
AE = [160 80 5.50 24 24 600 8 4 2.13 358.40 713.40];
BE = [170 100 6.50 36 48 700 8 4 1.83 307.20 732.20];
% Rotor Variables
CR = [70 0 3.00 0 0 300 12 4 6.40 1075.20 1225.20];
AR = [90 50 4.50 24 48 700 12 4 2.74 460.80 735.80];
BR = [100 80 4.00 36 36 500 12 4 3.84 645.12 925.12];
% Combat Variables
CC = [200 0 4.00 0 0 500 3 2 0.48 92.16 292.16];
CA = [240 70 3.80 12 36 900 3 2 0.27 51.20 311.20];
CB = [270 120 4.00 24 36 800 3 2 0.30 57.60 377.60];
CC = [300 160 4.50 48 48 750 3 2 0.32 61.44 446.44];
% Avionic Suite Variables
CA = [200 0 5.00 0 0 600 3 2 0.40 76.80 326.80];
AA = [240 50 4.40 24 24 900 3 2 0.27 51.20 321.20];
AB = [270 90 4.80 36 48 1000 3 2 0.24 46.08 376.08];
AC = [300 130 5.00 48 24 850 3 2 0.28 54.21 434.21];
%Permutations Values
X1 = [ CF ; CE ; CR ; CC ; CA ] ;
X2 = [ CF ; CE ; CR ; CC ; AA ] ;
X3 = [ CF ; CE ; CR ; CC ; AB ] ;
X4 = [ CF ; CE ; CR ; CC ; AC ] ;
X5 = [ CF ; CE ; CR ; CA ; CA ] ;
  3 Comments
Image Analyst
Image Analyst on 22 Jan 2016
How are X1 through X5 different from each other???? They seem to be the same set of C variables appended together.
Adam
Adam on 22 Jan 2016
They differ in the 5th column (or in the 4th column for X5)

Sign in to comment.

Answers (1)

Adam
Adam on 21 Jan 2016
Use an array, don't create 432 variables. That is the reason you are asking this question - you created all these variables and now you are immediately running into one of the many problems this creates.
If you had your variables in an array (use a cell array if the arrays in Xn are different lengths or contain different data types or just use a 2d array if they are all uniform in size and type) you would probably know exactly how to do what you want without even needing to ask.
Getting data into the right type of structures at the start is fundamental to writing neat and understandable code.
There are methods using the eval function where you can "make up for" bad data structure usage with unpleasant code constructs, but I have never used it and even if I have I would not recommend it for your own sake!
  3 Comments
Adam
Adam on 21 Jan 2016
X(1,:,:) = [CF; CE; CR; CC; CA];
X(2,:,:) = [CF; CE; CR; CC; AA];
etc.
would give you a 432*5*11 array (assuming all those listed arrays are length 11 - I didn't check) which you can index into to get each variable and can do maths on trivially without a loop e.g.
squeeze( sum( X ) );
will give you a 5 * 11 result array in which every variable has been summed over the 1st dimension.
I'm not sure I really understand from your question what kind of output you are looking for though.

Sign in to comment.

Categories

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

Tags

Community Treasure Hunt

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

Start Hunting!