is it possible to expand [0 {9}, 45 {2}, 0 {9}]

2 ビュー (過去 30 日間)
Triveni
Triveni 2016 年 8 月 9 日
コメント済み: Triveni 2016 年 8 月 10 日
I have a matrix like [0 {9}, 45{2}, 0{9}] i want to expand it like
[0 0 0 0 0 0 0 0 0 45 45 0 0 0 0 0 0 0 0 0]? and it works like loop, [ 0{k}, 45{k}, 0{k}]
please help.
  2 件のコメント
Image Analyst
Image Analyst 2016 年 8 月 9 日
What is [0 {9}, 45{2}, 0{9}]? Is it a cell array? Please give code for us to get [0 {9}, 45{2}, 0{9}] so we can then expand it.
Triveni
Triveni 2016 年 8 月 9 日
Actually, I have to solve large number of data i am irritate to copy and paste it from lyx.
when i copy and paste this gives me [-45_{2}, 120, 90, 60, 120, 90, -45, 90, -45, 60, 90_{2}, 45, 120, 90, 120, 60, 30, 45, 120, 45, 90, 60, 30, 45, 30_{3}, 120, 60_{2}, 30] this type of format.

サインインしてコメントする。

採用された回答

Image Analyst
Image Analyst 2016 年 8 月 9 日
If you're stuck starting with a string in that special format, then this code will parse it and produce the output vector
str = '[-45_{2}, 120, 90, 60, 120, 90, -45, 90, -45, 60, 90_{2}, 45, 120, 90, 120, 60, 30, 45, 120, 45, 90, 60, 30, 45, 30_{3}, 120, 60_{2}, 30]'
% Get rid of brackets
str(str== '[') = [];
str(str== ']') = [];
words = strsplit(str, ',')
output = [];
for k = 1 : length(words)
% See if there is a brace
thisWord = words{k}
braceLocation = strfind(thisWord, '{');
if isempty(braceLocation)
% There no brace there.
theNumber = str2double(thisWord);
theRepeat = 1;
else
% There is a brace there.
theNumber = str2double(thisWord(1:braceLocation-2));
theRepeat = str2double(thisWord(braceLocation+1:end-1));
end
fprintf('Repeat %d for %d times.\n', theNumber, theRepeat);
% Repeat this number the required number of times and append to the output.
output = [output, theNumber * ones(1, theRepeat)];
end
% Echo to command window:
output
  6 件のコメント
Image Analyst
Image Analyst 2016 年 8 月 9 日
You keep changing the format. Why is there no comma after the first 45? Or is it that the first one never has a comma but all the others do? Where are you getting these strings from anyway?
Triveni
Triveni 2016 年 8 月 10 日
Thanks, between Some sequences don't have comma. I'll change it.

サインインしてコメントする。

その他の回答 (3 件)

Steven Lord
Steven Lord 2016 年 8 月 9 日
If you write your problem slightly differently, you can use repelem.
v = repelem([0 45 0], [9 2 9])
  2 件のコメント
Triveni
Triveni 2016 年 8 月 9 日
then tell me program to convert "[0 {9}, 45{2}, 0{9}]" to "repelem([0 45 0], [9 2 9])", because my problem is remains same.
Triveni
Triveni 2016 年 8 月 9 日
can i print ([0 45 0], [9 2 9]) to "[0 {9}, 45{2}, 0{9}]"

サインインしてコメントする。


Sean de Wolski
Sean de Wolski 2016 年 8 月 9 日
This should do it:
str = '[0 {9}, 45{2}, 0{9}]';
expr = '(?<num>\d*)'; % match digits
nums = cellfun(@str2double,regexp(str,expr,'match')); % extract and convert
repelem(nums(1:2:end),nums(2:2:end)) % repelem
  2 件のコメント
Walter Roberson
Walter Roberson 2016 年 8 月 9 日
Not every entry has a repeat; also the repeats have an underscore between the number and the {
Triveni
Triveni 2016 年 8 月 9 日
Thanks, but unable to count negatives,
str = '[[0_{9}, 45_{2}, -45_{2}, 0_{7}]]';
expr = '(?<num>\d*)'; % match digits
nums = cellfun(@str2double,regexp(str,expr,'match'));
repelem(nums(1:2:end),nums(2:2:end)) % repelem

サインインしてコメントする。


Walter Roberson
Walter Roberson 2016 年 8 月 9 日
Paste it into a string, and then you could use string processing. For example you could use regexprep with dynamic regular expressions and the ${} output replacement construct.

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by