slicing matrix in efficient way

조회 수: 139 (최근 30일)
ARN
ARN 2020년 6월 5일
답변: James Tursa 2020년 6월 5일
I have a 120*1 matrix and i want to divide into 3 matrix x,y,z where first 10 elements goes to x, next 10 to y and next 10 to z and then next 10 elements to x as the second column. I did that
x = [1:10; 31:40; 61:70; 91:100] %=[10x4]
y = [11:20;41:50;71:80;101:.110] % =[10x4]
z= [21:30; 51:60; 81:90;111:120] %=[10x4]
Is there any efficient way to do that?

채택된 답변

Rik
Rik 2020년 6월 5일
Something like this should do the trick:
a=1:120;
b=reshape(a,10,3,[]);
x=squeeze(b(:,1,:));
y=squeeze(b(:,2,:));
z=squeeze(b(:,3,:));

추가 답변 (2개)

madhan ravi
madhan ravi 2020년 6월 5일
Matrix = reshape(1:120,10,[]);
x = Matrix(:,1:3:end)
y = Matrix(:,2:3:end)
z = Matrix(:,3:3:end)

James Tursa
James Tursa 2020년 6월 5일
Another way:
a = 1:120;
r = reshape(a,30,[]);
x = r( 1:10,:);
y = r(11:20,:);
z = r(21:30,:);

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by