How can I display specific number string ?
2 次查看(过去 30 天)
显示 更早的评论
Hello guys ! I need to import from a .csv and display only 10 numbers(bold ones) preceded by a certain 3 numbers(underlined) string, so this 10 numbers will we found with other value in a column more than one time but only the 3 number that precede are the same everytime. The column can contain 50 or more repetitive string of 3 numbers 21 22 23 with different afferent prices below.
The 3 numbers repetitive represent lot number and the following represent prices.
T = readtable('prices.csv');
B = T(:,2);
Example column no 2 from csv:
25.12
45.13
21
22
23
65.55
45.44
42.66
78.56
45.88
42.66
89.78
88.55
45.55
79.65
45.66
123.45
259.45
21.56
42.53
12.49
21
22
23
25.12
45.13
89.78
88.55
45.55
79.65
45.66
45.44
42.66
78.56
45.88
45.55
79.65
45.66
45.44
42.66
78.56
45.88
Thanks!
0 个评论
采纳的回答
Ameer Hamza
2020-6-13
Try this
M = [
25.12
45.13
21
22
23
65.55
45.44
42.66
78.56
45.88
42.66
89.78
88.55
45.55
79.65
45.66
123.45
259.45
21.56
42.53
12.49
21
22
23
25.12
45.13
89.78
88.55
45.55
79.65
45.66
45.44
42.66
78.56
45.88
45.55
79.65
45.66
45.44
42.66
78.56
45.88];
tf = ismember(M, [21; 22; 23]);
idx = find(diff(tf)>0)+1+(0:12);
vals = reshape(M(idx).', [], 1);
Result
>> vals
vals =
21.0000
22.0000
23.0000
65.5500
45.4400
42.6600
78.5600
45.8800
42.6600
89.7800
88.5500
45.5500
79.6500
21.0000
22.0000
23.0000
25.1200
45.1300
89.7800
88.5500
45.5500
79.6500
45.6600
45.4400
42.6600
78.5600
3 个评论
Ameer Hamza
2020-6-13
"So it must to be unique". How do you define that? The code only have 3 lines
tf = ismember(M, [21; 22; 23]);
idx = find(diff(tf)>0)+1+(0:12);
vals = reshape(M(idx).', [], 1);
You can write it as a function
function y = get10vals(M, indicators)
tf = ismember(M, indicators);
idx = find(diff(tf)>0)+1+(0:12);
vals = reshape(M(idx).', [], 1);
end
and then call it like this
M = % matrix
vals = get10vals(M, [21; 22; 23]);
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Startup and Shutdown 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!