2017-08-03 168 views
-2

有人可以解釋這裏發生了什麼嗎?我知道Y(:,1)是Y中值的第一列。 但Y(p,1)是什麼意思? Matlab的小白Matlab矩陣訪問列

Y = load('testFile.txt'); %Load file 
p = randperm(length(Y)); %List of random numbers between 0 and Y size 

Y(:,1) = Y(p,1); 
Y(:,2) = Y(p,2); 
Y(:,3) = Y(p,4); 

回答

1
Y = load('testFile.txt'); %Load file 
% create a mapping to shuffle the rows of Y. 
% This assumes more rows than columns in Y, otherwise the code doesn't make sense. 
% This is because length(Y) returns the length of the largest dimension. 
% A more robust implementation would use size(Y,1) rather than length(Y). 
p = randperm(length(Y)); %List of random numbers between 0 and Y size 

% Rearrange the rows of column 1 based on the shuffled order 
Y(:,1) = Y(p,1); 
% Rearrange the rows of column 2 based on the shuffled order 
Y(:,2) = Y(p,2); 
% Set the third column to the shuffled fourth column. 
Y(:,3) = Y(p,4); 
+0

謝謝@jodag – llebpmac