How do I horizontally concatenate a cell of strings with a table of numbers?

9 vues (au cours des 30 derniers jours)
James Stevens
James Stevens le 31 Juil 2015
Modifié(e) : polar le 20 Juin 2016
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 commentaires
Azzi Abdelmalek
Azzi Abdelmalek le 31 Juil 2015
Can you post a short example with expected result?
James Stevens
James Stevens le 31 Juil 2015
Modifié(e) : Azzi Abdelmalek le 1 Août 2015
Short example input:
456 256 -848 Jumping Jacks
728 224 -2096 Jumping Jacks
1392 400 -1928 Jumping Jacks
2296 1008 -2328 Jumping Jacks
3168 1152 -2224 Jumping Jacks
3120 1640 -1184 Jumping Jacks
2608 1584 -408 Jumping Jacks
Expected result:
[#xAvg #xStDev #yAvg #yStDev #zAvg #zStDev #accelAvg #accelStDev "Jumping Jacks"]
(the #xAvg would contain whatever the numerical average of the x column (for that window) is).
If there was more input it would take several windows to cross all the input, so it would output several rows of that form.

Connectez-vous pour commenter.

Réponses (1)

Madhav Rajan
Madhav Rajan le 4 Août 2015
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 commentaire
polar
polar le 20 Juin 2016
Modifié(e) : polar le 20 Juin 2016
I have this problem: I wrote a script for downloading a 4 stocks price's vector that i called "tickers". In my script I display a 4x4 matrix of all correlations. What do I have to do for displaying all tickers names together with the data? Thanks

Connectez-vous pour commenter.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by