How can I turn vector to scalar?

조회 수: 21(최근 30일)
Poohish 8 Nov 2019
Edited: Poohish 8 Nov 2019
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)

로그인 to comment.

답변 수 (1)

KSSV 8 Nov 2019
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

댓글 수: 5

표시 이전 댓글 수: 2
Poohish 8 Nov 2019
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.
KSSV 8 Nov 2019
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 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?

로그인 to comment.

이 질문에 답변하려면 로그인을(를) 수행하십시오.

Translated by