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

1 view (last 30 days)
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 Comments
Walter Roberson
Walter Roberson on 6 Dec 2021
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

Sign in to comment.

Accepted Answer

Mathieu NOE
Mathieu NOE on 6 Dec 2021
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

More Answers (1)

Matt J
Matt J on 6 Dec 2021
Edited: Matt J on 6 Dec 2021
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

Tags

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!