How do I use elements in a vector to create a number

If I have a vector with a certain size of elements how can I turn some of those elements into a combined number versus separate numbers in a vector?

댓글 수: 3

Please provide and example.
x=[1 2 3 4 9 7];
y=num2str(x(2:5));
y=str2double(y(y~=' '));%2349
Thank you but I was asking more along the lines of if you have
x=[1 2 3 4 9 7];
to create
number = 123
or number = 497
I know how to capture the elements I want from the vector but don't know how to extract them into a number.

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

 채택된 답변

David Hill
David Hill 2020년 9월 28일
Take whatever indexes of the vecture you want to create the number out of say 1:3
x=[1 2 3 4 9 7];
y=num2str(x(1:3));
y=str2double(y(y~=' '));%123
If the size of the numbers will be the same number of digits, you could do it all together and then reshape at the end.
x=[1 2 3 4 9 7];
y=num2str(x);
y=str2num(reshape(y(y~=' '),3,[])');%[123;497]

댓글 수: 1

OHH I see now, I didn't include that str2double at the end and it had it under 'appostraphies' so I wasn't satisfied at first but now that I include it I can see what you mean. Thank you!

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

추가 답변 (0개)

카테고리

태그

질문:

2020년 9월 28일

댓글:

2020년 9월 28일

Community Treasure Hunt

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

Start Hunting!

Translated by