Extracting Data from Table
6 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I am attempting to sort a large set of 3D data, into 2D frames. For context, the data I have imported is a text file and I want to seperate the text file into seperate tables, which all have the same X value. I had previously done this by creating several ID variables...
idx_250 = FFF1.x_coordinate == 0.25;
x_250 = FF1(idx_250,:);
This was repeated for each X value from 0.25 to 4, in 0.25 intervals. In attempt to neaten this up I tried to create a for loop.
for i = 1 :length(xv1)
id = FFF1.x_coordinate == xv1(i);
idTable = FFF1(id,:);
%Here I will process the data within idTable and save it in an appropriate array
end
Here, xv1 is a vector containing all of my x values [0.25 0.5 ... 4].
However, when i = 1 and xv1(i) therefore equals 0.25, the id vector only has one true value. However if I put: id = FFF1.x_coordinate == 0.25, there is no problem. Could someone please explain why it would work if i directly type 0.25 vs a variable with the same value? Thanks!
Réponse acceptée
Star Strider
le 27 Jan 2025
Modifié(e) : Star Strider
le 27 Jan 2025
It would help to have your data.
Lacking it, consider doing something like this —
T1 = table(reshape(repmat((1:5),5,1),[],1), sort(rand(25,1)), randn(25,1), VariableNames=["Class","X","Y"])
[UClass,~,idx] = unique(T1.Class,'stable');
XYc = accumarray(idx, (1:numel(idx)).', [], @(x){T1(x,:)})
XYc{3}
In this instance, all the ‘XYc’ tables are the same sizes, however they don’t have to be, and ‘Class’ do not have to be integers, providing there is a limited number of different values for it. (The'stable' argument simply returns them in the order the unique function encountered them, rather than sorting them as well.)
Example —
T1 = table(randi(5,25,1)*rand, sort(rand(25,1)), randn(25,1), VariableNames=["Class","X","Y"])
[UClass,~,idx] = unique(T1.Class,'stable');
XYc = accumarray(idx, (1:numel(idx)).', [], @(x){T1(x,:)})
XYc{1}
XYc{3}
XYc{5}
.
EDIT — Corrected typographical errors.
.
2 commentaires
Plus de réponses (1)
Voss
le 27 Jan 2025
If FFF1.x_coordinate == xv1(i) gives a different result than FFF1.x_coordinate == 0.25, then xv1(i) is not equal to 0.25. You don't show how xv1 is constructed, so I can't comment on why xv1(1) is not exactly 0.25.
Regardless, here is a way to split a table into a cell array of tables based on the value of one table variable (in this case 'x'):
x = randi(16,100,1)/4; % x: random values amongst {0.25, 0.5, ..., 3.75, 4}
y = randn(100,1); % y: some other random values
z = randn(100,1); % z: some other random values
T = table(x,y,z)
% split T into cell array of tables C, based on value of T.x
vars = T.Properties.VariableNames;
C = splitapply(@(varargin){table(varargin{:},'VariableNames',vars)},T,findgroups(T.x))
% show the tables contained in cell array C
C{:}
Voir également
Catégories
En savoir plus sur Data Distribution Plots dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!