How to transform a vector that have "*" to indicate that a number is repeated

조회 수: 1 (최근 30일)
hello,
I have a relative simple question... I have a vector A=[3*2 1 5*10] and I would like that this vector will be transformed in a column vector B=[2 2 2 1 10 10 10 10 10]'... How to do it in an efficient manner? By the way, the first number in A is the frequency of the following number is repeated...
Regards,
  댓글 수: 2
Fangjun Jiang
Fangjun Jiang 2021년 12월 6일
see repmat(), repelem(), as long as you can provide A in a recognizable format
Walter Roberson
Walter Roberson 2021년 12월 6일
is it a text vector? Because if you entered that into matlab you would not be able to distinguish between needing 3 3 or needing 2 2 2 or needing 1 1 1 1 1 1

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

채택된 답변

Mathieu NOE
Mathieu NOE 2021년 12월 6일
hello
my suggestion :
A='3*2 1 5*10';
As = split(A);
out_all = [];
for ci = 1:numel(As)
B = split(As{ci},'*');
BB = str2double(B);
if numel(B)>1 % then we have a multiplier
out = repmat(BB(2),1,BB(1));
else
out = BB;
end
out_all = [out_all out];
end

추가 답변 (1개)

Matt J
Matt J 2021년 12월 6일
편집: Matt J 2021년 12월 6일
A='3*2 1*1 5*10';
C=str2double(strsplit(A,{' ','*'}));
B=repelem(C(2:2:end), C(1:2:end))
B = 1×9
2 2 2 1 10 10 10 10 10

카테고리

Help CenterFile Exchange에서 Analog Devices ADALM1000 Support from Data Acquisition Toolbox에 대해 자세히 알아보기

태그

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by