Subdividing a matrix and pairing specific columns in the subdivisions

3 visualizaciones (últimos 30 días)
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

Respuesta aceptada

Image Analyst
Image Analyst el 26 de Ag. de 2021
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 comentarios
Image Analyst
Image Analyst el 27 de Ag. de 2021
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
Michel Nieuwoudt
Michel Nieuwoudt el 27 de Ag. de 2021
Thank you! Your help and input is very much appreciated.
Best wishes,
Michel

Iniciar sesión para comentar.

Más respuestas (1)

the cyclist
the cyclist el 26 de Ag. de 2021
Editada: the cyclist el 26 de Ag. de 2021
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 comentarios
Image Analyst
Image Analyst el 26 de Ag. de 2021
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 el 26 de Ag. de 2021
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

Iniciar sesión para comentar.

Categorías

Más información sobre Logical en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by