How can I turn vector to scalar?

Let say the example below
x=(10,20,30,40,50,60,70)
which mean
x(1)=10;
x(2)=20;
x(3)=30;
...
x(7)=70;
How can i define the variable using loop without mentioning or defining one by one ?
As i have x(1) up to x(1155) so I can sum x(1)+x(2)+...+x(1155)

Answers (1)

x = 10:10:70 ; % this will generate [10,20,..70] ;
sumx = sum(x) ; % this will give you sum of array
Also you can call each number from array by indexing.
for i = 1:length(x)
xi = x(i)
end

7 Comments

aNgie Liong commented:
I want as variables.
To Maximise the variables
YOu have them already right?
x(1)
x(2)
.
.
x(end)
yes I have x value but it is in a size of 1x1155.
Alright my real senario is ike this. I have A,B,C,D,E values all in size 1x1155.
A=[1.0001, 1.0005, 1.00010,...,1.0009. similar to B, C, D and E.
I need (A(1)+B(1))/(C(1)+D(1)E(1)) + (A(2)+B(2))/(C(2)+D(2)E(2)) + ... + (A(1155)+B(1155))/(C(1155)+D(1155)E(1155)). Therefore i need each individual variable using loop rather than defining one by one from 1 to 1155.
A = rand(1,1155) ;
B = rand(1,1155) ;
C = rand(1,1155) ;
D = rand(1,1155) ;
E = rand(1,1155) ;
F = (A+B)./((C+D).*E) ;
thesum = sum(F) ;
Poohish
Poohish on 8 Nov 2019
Edited: Poohish on 8 Nov 2019
Sorry this is not what i want.
Look the example of this:
The objective of the example is x(1) + x(2). It is easy to type manually.
But I have 1155 variable [meaning x(1)+x(2)+...+x(1155)] to optimise and it will take ages to type manually. Thus I must use loop.
My main part is to solve optivar('x',1,1155,'LowerBound',0,'UpperBound',0) - 1155 variables.
How to use the loop in here?
I could really use the answer to this question if anyone found out how to do this. I am having the same exact problem in my code currently
I suggest that you start a new question with details. I cannot understand why the previous answers suggesting to use sum are not adequate, so more detail is essential to get a helpful answer.
Alan Weiss
MATLAB mathematical toolbox documentation

Sign in to comment.

Asked:

on 8 Nov 2019

Commented:

on 31 Jul 2022

Community Treasure Hunt

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

Start Hunting!