How can I display specific number string ?

2 Ansichten (letzte 30 Tage)
Cristian Martin
Cristian Martin am 13 Jun. 2020
Kommentiert: Cristian Martin am 13 Jun. 2020
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!

Akzeptierte Antwort

Ameer Hamza
Ameer Hamza am 13 Jun. 2020
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 Kommentare
Ameer Hamza
Ameer Hamza am 13 Jun. 2020
"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]);
Cristian Martin
Cristian Martin am 13 Jun. 2020
Thanks a lot Amza, that's ok.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

Produkte


Version

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by