extracting smaller matrices from a larger matrix through loop
조회 수: 2(최근 30일)
표시 이전 댓글
hi all i have lets say a 100*2 matrix, like x data and corresponding y data. i want to divide or partition this in to 10 parts, like one part= x1 y1... x10 y10 2nd part x11 y11...x20 y20.. and so on. ie., i want to partition this 100*2 matrix in to ten 10*2 matrices. how can i do that through a loop? because i want each of these partitions and save them separately for plotting and other purposes. i am a beginner in matlab scripting. please could you help me? thanks in advance johan mark
댓글 수: 0
답변(3개)
Star Strider
2015년 1월 29일
This is not as efficient as I would like it to be, but it works:
M = [1:100; 501:600]'; % Created Data
M10 = reshape(M, [], 10, 2); % Reshape
M10 = permute(M10, [1 3 2]); % Shuffle Dimsnsions
Q3 = M10(:,:,1) % See First 10 Rows
Q4 = M10(:,:,2) % See Second 10 Rows
댓글 수: 3
Star Strider
2015년 1월 29일
The sincerest expression of appreciation here on MATLAB Answers is to Accept the Answer that most closely solves your problem.
Guillaume
2015년 1월 29일
I would use mat2cell to split your 100*2 matrix into a cell array of 10*2 matrices, and iterate over the cells:
m = [(1:100)' (201:300)']; %example matrix
submheight = 10; %height of submatrices
%the code only works if submheight is a divisor of the number of rows of m
%so make sure that is true:
assert(mod(size(m, 1), submheight) == 0)
rowdist = ones(1, size(m) / submheight) * submheight;
c = cell2mat(m, rowdist); %split into submatrices
for submatrix = c %iterate over cells
submatrix = c{1}; %convert cell to matrix
%... do whatever you want with submatrix, e.g:
plot(submatrix(:, 1), submatrix(:, 2));
end
댓글 수: 0
Andrei Bobrov
2015년 1월 29일
out = zeros(10,2,100/10);
for ii = 1:size(out,3)
out(:,:,ii) = M(k(ii)-9:k(ii),:);
end
댓글 수: 0
참고 항목
범주
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!