How I can split a matrix in four matrixes

10 ビュー (過去 30 日間)
Maryam Hamrahi
Maryam Hamrahi 2016 年 6 月 28 日
回答済み: David Sanchez 2016 年 6 月 28 日
I have a nx3 matrix and I would like to split it in four matrices with different number of rows and three columns. any help is appreciated.
  3 件のコメント
Stephen23
Stephen23 2016 年 6 月 28 日
So if the number of rows n is unknown, how many rows are in each of the four matrices ?
Have you tried using indexing, or mat2cell ?
Maryam Hamrahi
Maryam Hamrahi 2016 年 6 月 28 日
Number of rows in each matrices are not important. I am applying a code whcih contains a loop and it gives me error when the number of rows are more than 10000. So, I want to split the matrix to prevent the error.
I just need split the matrix in smaller matrices.

サインインしてコメントする。

採用された回答

KSSV
KSSV 2016 年 6 月 28 日
clc; clear all ;
A = rand(100,3) ; % rows should be greater then 4
a = size(A,1); % rows in A
n = 4; % number of partitions
out = diff([0,sort(randperm(a-1,n-1)),a]); % split rows into four random parts
% else you enter your own four numbers such that sum is size(A,1)
% four matrices
A1 = A(1:out(1),:) ;
A2 = A(out(1)+1:out(1)+out(2),:) ;
A3 = A(out(2)+1:out(2)+out(3),:) ;
A4 = A(out(3)+1:out(3)+out(4),:) ;

その他の回答 (1 件)

David Sanchez
David Sanchez 2016 年 6 月 28 日
M = rand(1001,3); % your matrix
L = size(M,1); % L = number of rows
n = floor(L/4); % number of rows for your matrices
N = cell(4,1);
for k=1:3
N{k,1} = M( ((k-1)*n+1):n*k,: );
end
N{4,1} = M((3*n+1):L,:);
% to access your matrices...
N_1 = cell2mat(N(1,1));

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by