automatic creating of array

Hi,
I would like automatic create a matrix, or array. My program should work like this:
If I insert any number n, then it will create matrix, or array with elements i1;i2;i3;..in with size nx1.
For example: if n=2; then result is [i1;i2],
ans =
i1
i2
if n=4; then result is [i1;i2;i3;i4].
Please can you help me?
Thank you

댓글 수: 4

Daniel Shub
Daniel Shub 2011년 10월 26일
You cannot have this. Well you probably can, but you would have to define your own data type. Do you want [i1;i2] or do you want [1i;2i]. The first is not valid MATLAB syntax, the second is (they are imaginary numbers). You could have ['i1';'i2'], but its size will be nx2. You will also run into problems when n gets to 10. You can also have {'i1';'i2'}, which will have size nx1 and won't fail when n gets to 10, but is not the same as [i1;i2].
More to the point, what have you tried so far?
Matt Tearle
Matt Tearle 2011년 10월 26일
Are i1, i2, etc variables? Or do you want the literal strings "i1", "i2", etc?
john
john 2011년 10월 26일
Daniel,
{'i1','i2'} will by perfect. Do you know do this?
Matt Tearle,
I use usually command sym for create i1, i2.... in M-file. For me 1,2,3 and i1,i2 are the same, mean the same.
But Matlab help is written: sym Define symbolic objects
So I don't know what is 1,2,3,4, and i1,i2...
Jan
Jan 2011년 10월 27일
@vansac: This is a public forum. Please post your questions here and not per email. Please take the time to read my profile.

댓글을 달려면 로그인하십시오.

 채택된 답변

Fangjun Jiang
Fangjun Jiang 2011년 10월 26일

0 개 추천

a=(1:n)'
or
a=cellstr(strcat('i',num2str((1:n)')))

댓글 수: 3

Daniel Shub
Daniel Shub 2011년 10월 26일
It fails the size nx1 requirement.
Fangjun Jiang
Fangjun Jiang 2011년 10월 26일
Good point. Updated!
john
john 2011년 10월 26일
Thank you so much Fangjun Jiang, good job :)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

태그

질문:

2011년 10월 26일

Community Treasure Hunt

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

Start Hunting!

Translated by