How do I horizontally concatenate a cell of strings with a table of numbers?
4 次查看(过去 30 天)
显示 更早的评论
Code:
function [finalTable] = featureExtractionTrainFileWise(varargin)
for masterVariable=1:nargin;%Goes over all the files
%Step 1: Load the data for one file into tables
windowWidth=64;%vary this to vary the size of the window
windowOverlap=0.5;
windowStep=windowWidth*windowOverlap;
table_x1 = [];
table_y1 = [];
table_z1 = [];
table_class1 = [];
table_accel1 = [];
t = readtable(varargin{masterVariable});
table_x1 = t.X_mG_;%contains numbers
table_y1 = t.Y_mG_;%contains numbers
table_z1 = t.Z_mG_;%contains numbers
table_class1 = t.Classification; %contains strings
[rows,~] = size(table_x1);
%Step 1a: Calculate acceleration for the data
table_accel1=zeros(rows,1); %Pre-allocate an array of all zeroes to save compilation time.
for i=1:rows
vector = [table_x1(i,1) table_y1(i,1) table_z1(i,1)];
table_accel1(i,1) = norm(vector);
end
%remove any extra rows (that won't fit in one window)
extraRows = mod(rows,windowWidth);
firstExtraRow = rows-extraRows +1;
table_x1(firstExtraRow:rows,:)=[];
table_y1(firstExtraRow:rows,:)=[];
table_z1(firstExtraRow:rows,:)=[];
table_accel1(firstExtraRow:rows,:)=[];
%Step 2: Slide a window down the columns and calculate the mean and
%standard deviation of each window (for each column)
numWindows = ceil(rows/windowStep);%num windows is the number of windows we will have, based on
%the number of rows we have, the amount of overlap, etc.
table_xMean = zeros(numWindows,1);
table_xStDev = zeros(numWindows,1);
table_yMean = zeros(numWindows,1);
table_yStDev = zeros(numWindows,1);
table_zMean = zeros(numWindows,1);
table_zStDev = zeros(numWindows,1);
table_accelMean = zeros(numWindows,1);
table_accelStDev = zeros(numWindows,1);
i=1;
while i<rows;
currentRow = floor(i/windowStep) + 1; %Returns 1 the first pass through the loop, 2 the second pass, 3 the third,etc.
firstPoint=i*windowStep;
lastPoint=i+windowWidth;
table_xMean(currentRow,1) = mean(table_x1(firstPoint:lastPoint,1),1); %average accel_x for the window
table_xStDev(currentRow,1) = std(table_x1(firstPoint:lastPoint,1),[],1); %standard deviation of accel_x for the window
table_yMean(currentRow,1) = mean(table_y1(firstPoint:lastPoint,1),1);%average accel_y for the window
table_yStDev(currentRow,1) = std(table_y1(firstPoint:lastPoint,1),[],1);%standard deviation of accel_y for the window
table_zMean(currentRow,1) = mean(table_z1(firstPoint:lastPoint,1),1);%average accel_z for the window
table_zStDev(currentRow,1) = std(table_z1(firstPoint:lastPoint,1),[],1);%standard deviation of accel_z for the window
table_accelMean(currentRow,1) = mean(table_accel1(firstPoint:lastPoint,1),1);%average accelartion for the window
table_accelStDev(currentRow,1) = std(table_accel1(firstPoint:lastPoint,1),[],1);%standard dev. of accel for the window
i = i + windowStep;
end
[number,~]=size(table_xMean);
table_class1(1:number,:)=[];
finalTable1=[table_xMean,table_xStDev,table_yMean,table_yStDev,table_zMean,table_zStDev,table_accelMean,table_accelStDev,table_class1];
finalTable=[finalTable;finalTable1];
end
end
Error:
Error using horzcat
Dimensions of matrices being concatenated are not consistent.
Error in featureExtractionTrainFileWise (line 98)
finalTable1=[table_xMean,table_xStDev,table_yMean,table_yStDev,table_zMean,table_zStDev,table_accelMean,table_accelStDev,table_class1];
If I remove the "table_class1" from the horizontal concatenation statement, so it would read:
finalTable1=[table_xMean,table_xStDev,table_yMean,table_yStDev,table_zMean,table_zStDev,table_accelMean,table_accelStDev];
then the error goes away, indicating that it is table_class1 that has inappropriate dimensions.
However, if my understanding is correct, it should have the same size as table_xMean, from:
[number,~]=size(table_xMean);
table_class1(1:number,:)=[];
I checked for off by one errors by trying:
table_class1(1:number+1,:)=[];
and
table_class1(1:number-1,:)=[];
But neither of those fixed it.
I suspect the problem has something to do with the fact that table_class1 is a cell array instead of a matrix (as dispcell(table_class1) prints it's output). I started using MATLAB today, so I'm not sure how to fix this, or if it's even a problem.
What can I do to concatenate table_class1 against the rest of the tables?
2 个评论
回答(1 个)
Madhav Rajan
2015-8-4
I undestand that you want to concatenate a cell of strings with a table of numbers. Looking at the script that you have provided, it appears that the following line:
finalTable1=[table_xMean,table_xStDev,table_yMean,table_yStDev,table_zMean,table_zStDev,table_accelMean,table_accelStDev,table_class1];
concatenates a cell array string with other vectors to form a matrix. It is not possible to concatenate a vector and a cell array of strings and form a matrix because all the elements must be of the same type. Suppose you have 'Age' and 'LastName' as two variables, one is a vector of numbers and the other is a cell array of strings.
Age = [38;43;38;40;49];
LastName = {'Smith';'Johnson';'Williams';'Jones';'Brown'};
If you concatenate them in the sameway as you did to obtain finalTable1, you would get the same error:
finalTable1 = [Age LastName]
Error using horzcat Dimensions of matrices being concatenated are not consistent.
Instead you can create a table out of these elements and assign that to finalTable1 as shown:
finalTable1 = table(Age, LastName);
Hope this helps.
1 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!