Import and reshape the data

I have 20000 data(in attachment) and data is in groups of 10 -- the data starts at 0 and returns to 0 each 10 entries.
I needs to plot them so I tried
readmatrix('trans_bad.txt', 'delimiter', '\t');
but it's not working. so I tried this one
r3 = importdata('trans_bad.txt');
ro = mean(reshape(r3,40,[]), 1);
ro = ro';
ys_03 =ro(1:max_iterations, 1);
plot(xs, ys_03);
not sure if my way is correct because the curve dropped into 0. Any suggestions?
thanks in advance!

2 comentarios

Brave A
Brave A el 5 de Sept. de 2022
Any idea? :)
Brave A
Brave A el 5 de Sept. de 2022
Any help ?

Iniciar sesión para comentar.

 Respuesta aceptada

Image Analyst
Image Analyst el 5 de Sept. de 2022
Looks like it's 0 every 20 elements, not 10.
Try this:
data = importdata('trans_bad.txt');
% Reshape into 20 by 1000 matrix.
data2 = reshape(data', 20, [])
data2 = 20×1000
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 3 4 3 2 3 2 2 3 2 3 3 2 3 3 3 3 2 2 2 3 2 2 2 2 2 2 2 2 2 11 10 12 8 7 9 7 7 9 7 8 9 6 8 9 8 8 7 7 6 8 6 7 6 7 7 6 6 7 5 22 21 23 15 15 18 15 14 17 14 16 17 13 16 18 14 15 15 14 12 15 12 13 12 14 14 12 12 15 10 35 34 37 24 26 29 25 23 28 24 26 28 22 26 29 22 25 25 23 20 24 19 21 20 23 24 20 20 25 17 51 50 53 36 40 41 37 34 41 36 38 41 33 38 42 31 36 37 34 30 34 28 31 30 33 36 29 29 38 26 69 69 71 49 56 55 51 47 56 51 51 55 46 52 56 41 50 50 46 42 46 38 43 41 45 50 40 40 52 36 89 90 92 64 75 71 67 62 72 68 66 71 60 68 72 52 66 64 60 55 59 50 56 53 58 65 52 52 68 48 112 113 115 81 96 89 84 78 90 87 83 89 76 85 90 65 83 80 75 70 73 63 71 66 72 82 66 65 85 61 137 138 139 100 119 108 103 96 110 108 101 108 93 104 109 79 102 97 92 86 88 77 87 80 87 100 81 79 103 76
[rows, columns] = size(data2)
rows = 20
columns = 1000
for row = 1 : rows
plot(data2(row, :), '-');
hold on;
end
grid on;

15 comentarios

Brave A
Brave A el 5 de Sept. de 2022
Editada: Brave A el 5 de Sept. de 2022
but this is not what I need. I really need them in one convergare curve not like this.
Brave A
Brave A el 5 de Sept. de 2022
Also I need them with 500 iterations as in my code.
Image Analyst
Image Analyst el 5 de Sept. de 2022
I just took every chunk of data starting at 0. If you have to combine two chunks in to one, with a total length of 40 instead of 20, then make the obvious modifications in my code by chaning 20 to 40.
I don't know what a "convergare curve" is. Even Google does not know. Please define that type of curve.
Brave A
Brave A el 5 de Sept. de 2022
just one line to represent the data.
I don't understand what you mean. A "convergare curve" is defined to be data that is plotted in only one line???
If you want just one line, plot it immediately after pulling it from the file without reshaping or anything.
data = importdata('trans_bad.txt');
plot(data)
grid on;
Please post an example of what you'd like to obtain but only after you read this:
Brave A
Brave A el 5 de Sept. de 2022
something like this.
Brave A
Brave A el 5 de Sept. de 2022
I know how to plot it immediatly. I post my question to help woth reshape the data and plot it on 500 iterations not 1000.
Image Analyst
Image Analyst el 6 de Sept. de 2022
Tell me again in words how you'd get this from the 1-D set of data you have. A different set of words because I think I did exactly what you said the first time, where a 0 started a new set of data.
Brave A
Brave A el 6 de Sept. de 2022
my original code gives one curve already(you can check) but I thnik I have a problem with reshaping the data.
Walter Roberson
Walter Roberson el 6 de Sept. de 2022
reshape by 20. Take the first 500 columns. Now you have 500 iterations instead of 1000.
But it is not at all clear what this has to do with drawing a single line. Do you need to do something such as take the mean of each group of 20, or plot the last value of each group of 20, or ??
Brave A
Brave A el 6 de Sept. de 2022
yes I need to take the mean of each group of 20 then plot it.
Walter Roberson
Walter Roberson el 6 de Sept. de 2022
ok so reshape into 20 rows, mean() along the first dimension, and plot()
Brave A
Brave A el 6 de Sept. de 2022
I tried 20 and 40 but doesnot give a good curve. Is there any other command instead of reshaping like taking windows size?
I tried this:
data = importdata('trans_bad.txt');
% Reshape into 20 by 1000 matrix.
data2 = reshape(data', 20, [])
data2 = 20×1000
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 3 4 3 2 3 2 2 3 2 3 3 2 3 3 3 3 2 2 2 3 2 2 2 2 2 2 2 2 2 11 10 12 8 7 9 7 7 9 7 8 9 6 8 9 8 8 7 7 6 8 6 7 6 7 7 6 6 7 5 22 21 23 15 15 18 15 14 17 14 16 17 13 16 18 14 15 15 14 12 15 12 13 12 14 14 12 12 15 10 35 34 37 24 26 29 25 23 28 24 26 28 22 26 29 22 25 25 23 20 24 19 21 20 23 24 20 20 25 17 51 50 53 36 40 41 37 34 41 36 38 41 33 38 42 31 36 37 34 30 34 28 31 30 33 36 29 29 38 26 69 69 71 49 56 55 51 47 56 51 51 55 46 52 56 41 50 50 46 42 46 38 43 41 45 50 40 40 52 36 89 90 92 64 75 71 67 62 72 68 66 71 60 68 72 52 66 64 60 55 59 50 56 53 58 65 52 52 68 48 112 113 115 81 96 89 84 78 90 87 83 89 76 85 90 65 83 80 75 70 73 63 71 66 72 82 66 65 85 61 137 138 139 100 119 108 103 96 110 108 101 108 93 104 109 79 102 97 92 86 88 77 87 80 87 100 81 79 103 76
[rows, columns] = size(data2)
rows = 20
columns = 1000
means = mean(data2, 1);
plot(means, 'b-', 'LineWidth', 2);
grid on;
What, exactly, is wrong with that? I know it doesn't match the picture you posted, but the picture you posted does not match what you're saying in words using the data you provided.
Is there any other command instead of reshaping like taking windows size?
Yes, you could use blkproc() to request to process 20 (or 40) samples at a time. The function is intended primarily for image processing, but there is no inherent reason why you could not use it for other block operations.
However, really if reshape() and mean() is not serving your needs, then I really doubt that blkproc() would be any more satisfactory, and using it would tend to obscure what you are doing. At this point I would suggest you use a for loop, like
for idx = 1 : 20 : numel(data)-19
thissegment = data(idx:idx+19);
%do something appropriate with the data in thissegment
end
This will provide clarity to you that the problem is not reshape() but rather a problem with your data or with your expectations.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Etiquetas

Preguntada:

el 5 de Sept. de 2022

Comentada:

el 6 de Sept. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by