Subdividing a matrix and pairing specific columns in the subdivisions
3 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Michel Nieuwoudt
il 26 Ago 2021
Commentato: Michel Nieuwoudt
il 27 Ago 2021
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
0 Commenti
Risposta accettata
Image Analyst
il 26 Ago 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 Commenti
Image Analyst
il 27 Ago 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
Più risposte (1)
the cyclist
il 26 Ago 2021
Modificato: the cyclist
il 26 Ago 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 Commenti
Image Analyst
il 26 Ago 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.
Vedere anche
Categorie
Scopri di più su Logical in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!