How to convert single column into multiple column having different spacing
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
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
Walter Roberson
on 29 Oct 2018
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
Shelender Kumar
on 29 Oct 2018
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.
Shelender Kumar
on 30 Oct 2018
Edited: Shelender Kumar
on 30 Oct 2018
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.
Shelender Kumar
on 31 Oct 2018
Hi, thanks a lot and it is pretty good. Thanks again
Shelender Kumar
on 1 Nov 2018
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), []);
Shelender Kumar
on 2 Nov 2018
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.
Shelender Kumar
on 2 Nov 2018
Thanks a lot I got it.
More Answers (0)
Categories
Find more on Timetables in Help Center and File Exchange
Tags
See Also
on 29 Oct 2018
on 2 Nov 2018
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)