24579

Accumulating different sized column vectors stored as a cell array into a matrix padded with NaNs

Imagine I have a series of different sized column vectors inside an array and want to group them into a matrix by padding the empty spaces with NaN. How can I do this?

There is already an answer to a very similar problem (accumulate cells of different lengths into a matrix in MATLAB?) but that solution deals with row vectors and my problem is with column vectors. One possible solution could be transposing each of the array components and then applying the above mentioned solution. However, I have no idea how to do this.

Also, speed is a bit of an issue so if possible take that into consideration.

Answer1:

You can just slightly tweak that answer you found to work for columns:

tcell = {[1,2,3]', [1,2,3,4,5]', [1,2,3,4,5,6]', [1]', []'}; %\\ ignore this comment, it's just for formatting in SO maxSize = max(cellfun(@numel,tcell)); fcn = @(x) [x; nan(maxSize-numel(x),1)]; cmat = cellfun(fcn,tcell,'UniformOutput',false); cmat = horzcat(cmat{:}) cmat = 1 1 1 1 NaN 2 2 2 NaN NaN 3 3 3 NaN NaN NaN 4 4 NaN NaN NaN 5 5 NaN NaN NaN NaN 6 NaN NaN

Or you could tweak this as an alternative:

cell2mat(cellfun(@(x)cat(1,x,NaN(maxSize-length(x),1)),tcell,'UniformOutput',false))

Answer2:

If you want speed the cell data structure is your enemy. For this example I will assume you have this vectors stored in a structure called vector_holder:

elements = fieldnames(vector_holder); % Per Dan request maximum_size = max(structfun(@max, vector_holder)); % maximum_size is the maximum length of all your separate arrays matrix = NaN(length(elements), maximum_size); for i = 1:length(elements) current_length = length(vector.holder(element{i})); matrix(i, 1:current_length) = vector.holder(element{i}); end

Many Matlab functions are slower when dealing with cell variables. In addition, a cell matrix with N double-precision elements requires more memory than a double-precision matrix with N elements.

Recommend

  • Computing 8 horizontal sums of eight AVX single-precission floating-point vectors
  • Add html blocks
  • Is there any big integer class for Visual Basic .NET (128 or more bits)?
  • Efficiently adding calculated rows based on index values to a pandas DataFrame
  • Left-handed calculations in OpenGL
  • What IDEs are available for R in Linux? [closed]
  • How to to parallelize the matrix transpose?
  • To run app in background for long time in iphone
  • mongodb: best way to get specific documents and then the rest
  • Date format jqplot categoryaxisrenderer after ticks add
  • Fake http server for testing purpose
  • Dropping support for JRE 1.3
  • Conditional Formatting in VBA, based on functions
  • Python: remove double quotes from JSON dumps
  • allow scroll of div set behind another div
  • VB.NET - RichTextBox - Apply formatting to selected text
  • Unity 5.1 Animator Controller not transitioning
  • How to get file download speed (transfer rate) with php?
  • Is there a way to call library thread-local init/cleanup on thread creation/destruction?
  • Classic ASP URL Rewriting
  • Efficient User-Agent Regex to find Safari in Python
  • How do I include a SWC in an AS2 Flash project?
  • Zurb Foundation _global.scss meta styles for js?
  • Marklogic : Query response time is very high
  • Allowing both email and username for authentication
  • Get one-time binding to work for ng-if
  • Change JButton Shape while respecting Look And Feel
  • Display issues when we change from one jquery mobile page to another in firefox
  • NetLogo BehaviorSpace - Measure runs using reporters
  • Read text file and split every line in MSBuild
  • Different response to non-authenticated users and AJAX calls
  • C# - Serializing and deserializing static member
  • Java applet as stand-alone Windows application?
  • Menu Color Fade on Hover with Jquery
  • Updating server-side rendering client-side
  • Arrow is showed instead of the material design version hamburger icon. Why doesn't syncState in
  • How do you troubleshoot character encoding problems?
  • Data Validation Drop Down Box Arrow Disappearing
  • Error creating VM instance in Google Compute Engine
  • Django query for large number of relationships