How to convert single column into multiple column having different spacing

Hi, I have a data file which is quite big, I want to reshape my data file but problem is that my data point is not uniformly spaced. For example, in this case, I want to have 1500 rows and 300 columns and each column must start from different temperature as in the first column of the raw file.
M = dlmread('MHfinal.dat', '', 2, 0); %%2, 0 represent from you want to start.
M1 = reshape(M,1542,7);

 Accepted Answer

uniquetol() applied to the temperature column. The third output is the grouping variable. Use
Output = accumarray(TheGroupingVariable, ThirdColumn, [], @(v){v.'});
If the resulting cell array happens to have the same number of entries in each cell then cell2mat. But it looks to me as if that is not likely to be the case: it looks to me as if your first temperatures do not repeat.

10 Comments

I did not understand, could you elaborate please
t = readtable('MHtry.txt');
[unique_temperatures, ~, TheGroupingVariable] = uniquetol(t{:,1});
Output = [num2cell(unique_temperatures), accumarray(TheGroupingVariable, t{:,3}, [], @(v) {v.'})];
Now Output is a cell array in which the first column contains temperatures (from the first column of your file) and the second column is a cell containing a list of all of the Moment Enu readings that have the same temperature.
About 5800 of the temperatures are not repeated. About 1400 of the temperatures occur exactly twice. The most occurrences is 8, which happens once.
It is not possible to group to 1500-ish rows of 300 columns by temperature, unless possibly you considerably relax what counts as a matching temperature. But even then it is difficult to get a match of that size.
With the data you uploaded, the data does appear to divide approximately equally into 7 bins of temperature, each with roughly 1530-ish entries.
Sorry, I do not think that I made myself clear enough. What I wanted was that 7 columns from the last two columns from the raw data (which you mentioned in the last line) and each column correspond to particular constant temperature with the error of 200 mK. Just to add on I am attaching a file which will give you an idea what I need.
t = readtable('MHtry.txt');
[bincounts, binedges, binnum] = histcounts(t{:,1}, 7);
maxcount = max(bincounts);
Output2 = nan(maxcount, 7);
Output3 = nan(maxcount, 7);
for K = 1 : 7
thismatch = binnum == K;
theseentries = t{thismatch, 2};
Output2(1:length(theseentries), K) = theseentries;
theseentries = t{thismatch, 3};
Output3(1:length(theseentries), K) = theseentries;
end
You will notice that Output3 contains several NaN at various locations. Those appear in the original data, such as the entry
293.9656677 -0.0550861321389675 NaN
at line 9200 of the input file.
The nan that appear at the bottom of the data are due to the fact that the data does not split equally into 7 bins by temperature. There are 1515 entries at the lowest temperature, up to 1536 entries at the highest temperature.
Hi, thanks a lot and it is pretty good. Thanks again
Hi, I have another problem, how do I combine one coloumn with another coloumn, in this case Output2's 1st coloumn with Output3's 1st coloumn side by side and then so on. Thanks
Where should the pairs of column 2 be located relative to the pair of column 1 ?
combined = reshape([Output2; Output3], size(Output2, 1), []);
Thanks a lot, that was what I needed. Sorry I did not get Where should the pairs of column 2 be located relative to the pair of column 1 ?
Do you want separate variables
Output_1 = [Output2(:,1), Output3(:,1)];
Output_2 = [Output2(:,2), Output3(:,2)];
Output_3 = [Output2(:,3), Output3(:,3)];
...
Output_7 = [Output2(:,7), Output3(:,7)];
Or do you want a single array,
Output = [Output2(:,1), Output3(:,1), Output2(:,2), Output3(:,2), Output2(:,3), Output3(:,3), ... Output2(:,7), Output3(:,7)];
The code I posted for combined builds the second of those possibilities.

Sign in to comment.

More Answers (0)

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!