How to transform a vector that have "*" to indicate that a number is repeated
1 view (last 30 days)
Show older comments
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
Fangjun Jiang
on 6 Dec 2021
see repmat(), repelem(), as long as you can provide A in a recognizable format
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
Accepted Answer
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
See Also
Categories
Find more on Analog Devices ADALM1000 Support from Data Acquisition Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!