Loop through every nth row in a data frame in a function

9 Ansichten (letzte 30 Tage)
I want to loop through specific rows to pick up data for some program I wrote and wondering if there is an efficient way of doing that. I have my coordinates in a matrix and each row represents a subject and the colums are the different orientation coordinates for a subject (see below)
In my loop I want to go through each row (each row and pick e.g "nose" for for each subject)
%% Define coil position
S.poslist{1}.pos(1).matsimnibs = [-m0n0 m1n0 -m2n0 x; -m0n1 m1n1 -m2n1 y; -m0n2 m1n2 -m2n2 z; 0 0 0 1]
I want to achieve something like this below but selecting every 4th row
S.poslist{1}.pos(1).matsimnibs = [-TargetInfo.data(4) TargetInfo.data(7) -TargetInfo.data(10) TargetInfo.data(1); -TargetInfo.data(5) TargetInfo.data(8) -TargetInfo.data(11) TargetInfo.data(2); -TargetInfo.data(6) TargetInfo.data(9) -TargetInfo.data(12) TargetInfo.data(3); 0 0 0 1]

Akzeptierte Antwort

Sulaymon Eshkabilov
Sulaymon Eshkabilov am 22 Mär. 2023
The simpler and more efficient way would be using a logical indexing option or just use necessary indices, e.g.:
% DATA input/generation as a table array
TS = categorical({'Boy'; 'Boy';'Boy';'Boy'; 'Girl';'Girl';'Girl';'Girl';});
TN = categorical({'A'; 'B'; 'C'; 'D';'A'; 'B'; 'C'; 'D'});
DATA=table(TS);
DATA.TName=TN;
M1 = randn(numel(TN),1);
M2 = randn(numel(TN),1);
M3 = randn(numel(TN),1);
M4 = randn(numel(TN),1);
M5 = randn(numel(TN),1);
M6 = randn(numel(TN),1);
M7 = randn(numel(TN),1);
M8 = randn(numel(TN),1);
M9 = randn(numel(TN),1);
M10 = randn(numel(TN),1);
M11 = randn(numel(TN),1);
M12 = randn(numel(TN),1);
DATA.M1 = M1;
DATA.M2 = M2;
DATA.M3 = M3;
DATA.x = M4;
DATA.M5 = M5;
DATA.M6 = M6;
DATA.M7 = M7;
DATA.y = M8;
DATA.M9 = M9;
DATA.M10 = M10;
DATA.M11 = M11;
DATA.z = M12
DATA = 8×14 table
TS TName M1 M2 M3 x M5 M6 M7 y M9 M10 M11 z ____ _____ __________ _________ ________ ________ ________ ________ ________ _________ ________ ________ ________ ________ Boy A 0.94176 0.96592 0.83127 -0.96469 -1.1138 0.95343 0.29157 -0.2933 0.13869 -1.1075 0.59653 -0.1007 Boy B -0.0085146 1.4973 0.85337 -1.7562 -2.1965 1.9926 -0.10816 1.2715 -0.21628 0.23034 0.75606 0.085673 Boy C -0.13105 -0.19626 0.75003 -0.97582 0.022491 -0.1896 0.80425 -0.17841 0.11202 0.77753 0.32635 -0.64259 Boy D -0.18264 -2.2729 -0.39656 1.9849 0.1503 0.26151 -0.87482 1.0075 0.60166 0.43676 0.64651 0.12189 Girl A 0.56582 -2.5636 -0.88507 -0.95755 -1.2312 0.42936 -0.39495 -1.0043 2.1618 -0.14574 -0.8057 2.0259 Girl B 0.46113 0.4177 0.37114 0.12721 -0.42584 -0.19921 0.94893 1.3251 0.080742 -0.98153 0.052248 0.34372 Girl C -0.78052 -0.029752 -1.2213 -0.91497 -0.18431 0.12213 0.52505 -0.22272 -1.0057 -2.6867 -1.2729 -1.9098 Girl D 1.0694 -1.6838 -0.2042 0.36261 -0.22849 -0.15683 -0.49298 -0.038873 -0.88004 0.76735 0.16935 1.6251
% Logical indexing is used to take only 'A' or in your exercise: 'Nose'
% Note TName is a categorical data
CA=table2array(DATA(DATA.TName=='A', 3:end));
% Take out the necessary data and make some negative and leave some
% unchanged
Nose = [reshape(CA, 6, 4); 0 0 0 1].*[-1 1 -1 1]
Nose = 7×4
-0.9418 -0.9647 -0.2916 -1.1075 -0.5658 -0.9576 0.3949 -0.1457 -0.9659 -1.1138 0.2933 0.5965 2.5636 -1.2312 1.0043 -0.8057 -0.8313 0.9534 -0.1387 -0.1007 0.8851 0.4294 -2.1618 2.0259 0 0 0 1.0000
% Alternative way:
CA2=table2array(DATA(1:4:end, 3:end));
Nose2 = [reshape(CA2, 6, 4); 0 0 0 1].*[-1 1 -1 1]
Nose2 = 7×4
-0.9418 -0.9647 -0.2916 -1.1075 -0.5658 -0.9576 0.3949 -0.1457 -0.9659 -1.1138 0.2933 0.5965 2.5636 -1.2312 1.0043 -0.8057 -0.8313 0.9534 -0.1387 -0.1007 0.8851 0.4294 -2.1618 2.0259 0 0 0 1.0000

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Produkte


Version

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by