Reshape list into columns

3 ビュー (過去 30 日間)
Joel Schelander
Joel Schelander 2021 年 3 月 2 日
回答済み: Gargi Patil 2021 年 4 月 15 日
Im working with driving data for vehicles. There is 429 vehicles (DeviceStats). For the trips, there is a list of 107910 rows with one value per row (Car). The value is the id number of the vehicles. The format of the list looks like this
173
173
173
178
178
184
and so on. Every vehicle has a different number of vehicles in the Car vector. I want to reshape the list to 429 columns, where each column has the length the same length as the vehicle has in the Car-list. Like this
173 178 184
173 178
173..
My approach so far has not worked:
clc
clear
load EXJOBB
D=[];
Car=tripStats.device;
Devicestats=deviceStats.device;
for j=1:429
for i=1:107910
if Car(i)==Devicestats(j)
D(i,j)=[Car(i)];
else
break
end
end
end
  3 件のコメント
Stephen23
Stephen23 2021 年 3 月 2 日
Matrices must be rectangular, i.e. all rows have the same number of columns, all columns have the same number of rows.
Gaps, missing data, holes, rows or columns of different lengths are not supported.
Do you want to:
  • pad the columns with some default value, e.g. 0, NaN, etc. (if so, what value?)
  • use a container array (e.g. cell array) to hold numeric vectors of different lengths?
Joel Schelander
Joel Schelander 2021 年 3 月 2 日
Yes, padding with 0 seems legit.
First vehicle has 224 trips, second has 136. First vehicle has id number 173, second id-number 178
I tried with this for-loop
A=[];
for j=1:429
for i=1:107910
if Car(i)==Devicestats(j)
A(i,j)=[Car(i)}]
elseif Car(i)<Devicestats(j)
break
end
end
end .
So in the end I want a cell array which contains the vectors of different sizes. This code only result in the first vehicle (224*1)

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

採用された回答

Gargi Patil
Gargi Patil 2021 年 4 月 15 日
I understand that you would like to create a matrix with each column containing same elements and each row having unique elements.
You have asked a similar question here which has been answered:
To extend the above solution further:
list = [173 173 173 178 178 184];
%Get unique elements in list
uniqueEle=unique(list(:,1));
num = numel(uniqueEle);
res = zeros(num,num);
ii = 1;
for i = 1:numel(uniqueEle)
output = list(list(:,1)==uniqueEle(i),:);
%Append zeros to make output of length num
output((end+1):num,1)=0;
res(:, ii) = output;
ii = ii + 1;
end
res will return the desired output matrix.
The rows can be converted to cell arrays using num2cell().

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by