필터 지우기
필터 지우기

How to split vector to columns based on index?

조회 수: 7 (최근 30일)
Maaz Madha
Maaz Madha 2021년 2월 24일
편집: KALYAN ACHARJYA 2021년 2월 24일
Hi all
Hi all
How do I split this vector in such a way that everytime the consecutive value is less than the one before the vector would split into different columns based on that index. I've tried writing this code to get the index of where the change happens but it doesnt work and I don't know how to split the vector into different columns.
for k=1:length(SOC2_stop)
if SOC2_stop(k+1)<SOC2_stop(k)
index(k)=k;
end
end
  댓글 수: 1
Bob Thompson
Bob Thompson 2021년 2월 24일
I recommend finding all of your desired breaks in one go with find.
index = (find(SOC2_stop(2:end)-SOC2_stop(1:end-1)<0))+1;
As for the splitting into new columns others probably have much fancier ways to accomplish that than I would, but if you want it all in one matrix then you're going to need to identify the maximum number of elements to fit in one column first.
nrows = max(index(2:end)-index(1:end-1));
newarray = nan(nrows,numel(index)+1);

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

답변 (1개)

KALYAN ACHARJYA
KALYAN ACHARJYA 2021년 2월 24일
편집: KALYAN ACHARJYA 2021년 2월 24일
diff_data=diff(column_data);
idx=find(diff_data<0)
idx gives the indices of raspective array elements, where present value is greater than next one. Afterwards, use the mat2cell function to segemnt the array in different cell array elements (Cell array is useful to save multiple arrays with different sizes).
  댓글 수: 2
Maaz Madha
Maaz Madha 2021년 2월 24일
I'm sorry I'm still new to matlab but I don't know how to use mat2cell to split the vector into columns
KALYAN ACHARJYA
KALYAN ACHARJYA 2021년 2월 24일
Please see the mat2cell function in MATLAB Doc (Do Google)
Other Way using loop (Not so efficient)
result=cell(1,length(idx))
for i=1:length(idx)-1
result{i}=data(idx(i):idx(i+1)-1);
end
result{end}=data(idx(end):length(data));
result

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

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by