Splitting up arrays in for loops

조회 수: 3 (최근 30일)
Ellen
Ellen 2020년 5월 27일
답변: Nadya Owusu 2022년 11월 10일
Hi!
I am using arrays that I would like to split up based on the values in the THIRD column. For example this array:
117 299 126
130 305 126
134 299 127
131 293 127
134 299 128
129 295 128
115 296 132
121 298 132
I would like to split is up at the place where the difference between the third column values is higher than 1 (between the current value and the value underneath it). In this case I would like to end up with the following two arrays:
117 299 126
130 305 126
134 299 127
131 293 127
134 299 128
129 295 128
and
115 296 132
121 298 132
I would also like to apply this to a large dataset so not doing it manually.
I thought about using if else statements in a for loop but I don't know how to how to calculate the difference between the third column values.
Can someone help me in how to start with this?
Thank you!

채택된 답변

David Hill
David Hill 2020년 5월 27일
n=[0,find(diff(M(:,3))>1),size(M,1)];
for k=1:length(n)-1
m{k}=M(n(k)+1:n(k+1),:);
end
  댓글 수: 1
Ellen
Ellen 2020년 5월 27일
Yes I got it now, thank you!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Nadya Owusu
Nadya Owusu 2022년 11월 10일
disp("Question 1:")%Displays question number.
a = 210;%The upper limit of the integral.
b = 0;%Lower limit of the integral.
n = 14;%Number of trapezoid sub-intervals in other words number of sections used.
h = (b-a)/n;%Length of trapezoid.
t = b:n:a;
f = (-3.19892*10.^-8)*(t.^7)+(2.77042*10.^-5)*(t.^6)+(-0.00928)*(t.^5)+(1.49974)*(t.^4)+(-1.18729*10.^2)*(t.^3)+(4.25940*10.^3)*(t.^2)+(-5.42886*10.^4)*(t)+(3.50829*10.^6);%Flow rate(t) equation.
integral = 0;
I want to split up the data from the for loop and add it to an array. please help
for t = b:n:a
At least one END is missing. The statement beginning here does not have a matching end.

Error in connector.internal.fevalMatlab

Error in connector.internal.fevalJSON
array = split(f);
disp(array)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by