Subdividing a matrix and pairing specific columns in the subdivisions

2 次查看(过去 30 天)
Hi experts,
I have a matrix of 1001 columns and 6000 rows. The first column is frequency, and the rest are signal intensities of a 1000 different samples. I want to create individual xy columns for the 1000 samples (so 1000 individual xy files), with the first column as x in each and the second column as each subsequent y value.
So D=[x y1 y2 y3 .....y1000], where each of these entries is a column of 6000 datapoints and I would like xy, xy1, xy2, xy3....xy1000 where xy=6000 x 2.
How would I go about this please?
Thank you in advance!
Best wishes,
Michel

采纳的回答

Image Analyst
Image Analyst 2021-8-26
Try this:
% Create sample data
data = rand(6000, 4);
% Sort first columns
data(:, 1) = sort(data(:, 1), 'ascend');
% Now we have our data and can begin.
[rows, columns] = size(data)
freq = data(:, 1); % Frequency is column 1.
for col = 2 : columns
fileName = sprintf('Column %3d.txt', col);
fprintf('Writing column %d to "%s".\n', col, fileName);
% Create N-by-2 matrix of frequencies in column 1, then signal in column 2.
xy = [freq, data(:, col)];
% Write out data to the text file.
writematrix(xy, fileName);
end
  3 个评论
Image Analyst
Image Analyst 2021-8-27
Yes. Let's say that the names you already have are stored in a cell array variable called columnNames. Then just use that for the filename:
% Now we have our data and can begin.
[rows, columns] = size(data)
freq = data(:, 1); % Frequency is column 1.
for col = 2 : columns
fileName = sprintf('%s.csv', columnNames);
fprintf('Writing column %d to "%s".\n', col, fileName);
% Create N-by-2 matrix of frequencies in column 1, then signal in column 2.
xy = [freq, data(:, col)];
% Write out data to the text file.
writematrix(xy, fileName);
end

请先登录,再进行评论。

更多回答(1 个)

the cyclist
the cyclist 2021-8-26
编辑:the cyclist 2021-8-26
You should strongly reconsider whether you need dynamically named variables, because it is usually a terrible idea. Instead, consider making a 3-dimensional array. Then, instead of the variable xy7, you can just access xy(:,:,7). Your code will be more robust.
% Pretend input
D = rand(6000,1001);
% Initialize 3-dimensional array
xy = zeros(6000,2,1000);
% Fill first "slice" in 2nd dimension with copies x
xy(:,1,:) = repmat(D(:,1),[1 1 1000]);
% Fill second slice with y
xy(:,2,:) = reshape(D(:,2:end),6000,1,1000);
  2 个评论
Image Analyst
Image Analyst 2021-8-26
Do you think he really meant that, because like you said, that would be a terrible idea?
I thought he wanted to just save them (each xy 6000-by-2 matrix) to 1000 files or somewhere rather than in 1000 separate, individually named variables.
Michel Nieuwoudt
Michel Nieuwoudt 2021-8-26
This is true, I do need to have separate files that I can plot individually in a spectroscopic software that won't plot matrices, so that I can perform band resolutions, etc. where needed.
Thank you to the cyclist for the suggestion though. I very much appreciate your answers!
Best wishes,
Michel

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Annotations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by