if i need to create a new number from two given numbers for example : a=4 b=9 new number should be a times b (9999). is there a command to do that ?

 채택된 답변

Andrew Reibold
Andrew Reibold 2014년 12월 2일
편집: Andrew Reibold 2014년 12월 2일

0 개 추천

Judging by your example, I believe you mean b repeated a times. Not a times b.
Yes there is a command for that.
repmat(b,1,a)
The first number is what is to be repeated. The second number is how many rows to be repeated across. The third number is how many columns to be repeated across.
Edit : This actually doesn't combine them into one number but puts it into a matrix. Combine after repeating? Not sure how to do in one step. Maybe another approach out there. If you don't mind turning the result into a string, combining them, then turning it to a number, that's what I would do.

댓글 수: 2

Alex
Alex 2014년 12월 2일
this way it returns as a vector, but i need it to be a single integer.
Alex
Alex 2014년 12월 2일
i figured it out. tnx. i can print it out as i needed it using fprintf

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

추가 답변 (2개)

Guillaume
Guillaume 2014년 12월 2일

2 개 추천

Using conversion to string and back this is easy but maybe not fast:
repeat = 3;
number = 12;
repeatednumber = str2num(repmat(num2str(number), 1, repeat))

댓글 수: 1

Or staying exclusively numeric:
repeat = 3;
number = 12;
temp = repmat(number, 1, repeat);
repeatednumber = sum(temp .* 10.^((numel(temp)-1:-1:0)*(1+floor(log10(number)))))

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

Andrei Bobrov
Andrei Bobrov 2014년 12월 2일
편집: Andrei Bobrov 2014년 12월 2일

0 개 추천

sum(10.^(a-1:-1:0)*b)
or
polyval(b*ones(1,a),10)
add
polyval(b*ones(1,a),10^ceil(log10(b)))

카테고리

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

제품

질문:

2014년 12월 2일

편집:

2014년 12월 2일

Community Treasure Hunt

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

Start Hunting!

Translated by