How can I rearrange this matrix into new one?

조회 수: 1 (최근 30일)
HyoJae Lee
HyoJae Lee 2023년 1월 25일
댓글: HyoJae Lee 2023년 1월 25일
data=[50 67.5;
50 67.9;
60 75.4
60 75.9;
70 12.3;
70 12.9];
Hello, this is a data what I want to rearrage into new one.
First column of the data is time, and second is value.
This matrix is sepearated every two rows, first is starting value and second is end value.
I want to rearrage into
  1. fix the time
  2. expand the value (starting value to end value every 0.1)
Finally, I want to make this kind of data,
data=[50 67.5;
50 67.6;
50 67.7;
50 67.8;
50 67.9;
60 75.4;
60 75.5;
60 75.6;
60 75.7;
60 75.8;
60 75.9;
70 12.3;
70 12.4;
70 12.5;
70 12.6;
70 12.7;
70 12.8;
70 12.9];
How can I make like this using loop?
Best,
HyoJae.

채택된 답변

Dyuman Joshi
Dyuman Joshi 2023년 1월 25일
Assuming data is homogenous (In pairs throught-out), and end value is greater than or equal to first value.
data=[50 67.5;
50 67.9;
60 75.4
60 75.9;
70 12.3;
70 12.9];
row=size(data,1)/2;
%using cell array to store generated arrays
out=cell(row,1);
for i=1:row
y=(data(2*i-1,2):0.1:data(2*i,2))';
out{i}=[repelem(data(2*i-1,1),numel(y),1) y];
end
cell2mat(out)
ans = 18×2
50.0000 67.5000 50.0000 67.6000 50.0000 67.7000 50.0000 67.8000 50.0000 67.9000 60.0000 75.4000 60.0000 75.5000 60.0000 75.6000 60.0000 75.7000 60.0000 75.8000
  댓글 수: 2
Askic V
Askic V 2023년 1월 25일
This is another approach:
data=[50 67.5;
50 67.9;
60 75.4
60 75.9;
70 12.3;
70 12.9];
step = 0.1;
time_col = unique(data(:,1));
data_new = [];
for i = 1:numel(time_col)
ind_i = find(data(:,1)==time_col(i));
sec_column = data(ind_i(1),2):step:data(ind_i(end),2);
first_column = time_col(i)*ones(size(sec_column));
data_new = [data_new; [first_column', sec_column']];
end
data_new
data_new = 18×2
50.0000 67.5000 50.0000 67.6000 50.0000 67.7000 50.0000 67.8000 50.0000 67.9000 60.0000 75.4000 60.0000 75.5000 60.0000 75.6000 60.0000 75.7000 60.0000 75.8000
HyoJae Lee
HyoJae Lee 2023년 1월 25일
Wow, Joshi, thanks for your reply!
This is actually what I want.
Appreciated!

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

추가 답변 (0개)

카테고리

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