Clear Filters
Clear Filters

user defined variable input

3 views (last 30 days)
Anik SARKER on 5 Mar 2018
Commented: Anik SARKER on 5 Mar 2018
If i have sample formula k=aX1*aX2*aX3*......*aXn. here X1,X2,...Xn are vector of X=[X1 X2 ... Xn]... if user input has only 2 component X1,X2 the output will be k=aX1*aX2. if user has n component in the vector then the output will be k=aX1*aX2*aX3*......*aXn. how to solve such a case

Accepted Answer

ES on 5 Mar 2018
Get the user input of X using input function. Then get the length of X using length function.
Run a for loop on length of X and multiply with 'a'.
Stephen23 on 5 Mar 2018
Note that it is recommended to avoid length, and only use numel or size. This avoids bugs caused by length measuring different dimensions without warning.

Sign in to comment.

More Answers (0)


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


Community Treasure Hunt

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

Start Hunting!