Rearranging by Matlab


A = [a 2 5 8; b 4 8 NaN]

where a and b are 1x3 row vectors.

I need to get a cell array B with:

B{1}=2 a B{2}=5 a B{3}=8 a b B{4}=4 b

The ordering doesn't matter though.

So I need to put the 4,5,6..th columns element by element with respect to those 1x3 row vectors a and b, disregarding NaN.

My first try was unique(A), but this alone couldn't eliminate NaN, nor can it match correctly.

Perhaps I also need to get the index matrix of at which "row" each element (2,5,8,4,8,), but I couldn't find how.

I then tried using for and if. But my PC was unable to process this with huge file size.


So you have a matrix A:

A = [a 2 5 8; b 4 8 NaN];

I'll first split the matrix into parts consisting of a and b and the rest:

a_and_b = A(:,1:3); Arest = A(:,4:end);

then we'll see what the unique items are in this Arest matrix, and remove the NaNs:

Arest_uniq = unique(Arest); Arest_uniq = Arest_uniq(~isnan(Arest_uniq));

check the occurences of the elements in Arest_uniq in rows of Arest:

occur_A = arrayfun(@(ii) ismember(Arest_uniq,Arest(ii,:)),1:size(A,1),'uni',false);

Because adding those rows a and/or b based on an if-construction isn't a linear operation, I'd rather just do it in a loop.

output = num2cell(Arest_uniq); for ii=1:numel(output) for jj=1:size(A,1) if occur_A{jj}(ii) output{ii} = [output{ii} a_and_b(jj,:)]; end end end

Go through this with <a href="http://www.mathworks.nl/help/matlab/matlab_prog/debugging-process-and-features.html#brqxeeu-192" rel="nofollow">step-by-step debugging</a>, inspect variables on the way, and eventually you'll understand what everything does. So next time you can solve your problems yourself on the spot.


  • What are the reordering columns on laravel-backpack?
  • Android Permission Denial: broadcasting Intent
  • Functional Programming in Swit to distribute array elements to correct “buckets”
  • How to make local USB port to work with Azure virtual machine
  • InputDispatcher Error
  • Background image does not show on iPhone/iPad
  • Pointer vs Reference difference when passing Eigen objects as arguments
  • Not able to display correct data in table -AngularJS
  • Best practice to eliminate magic numbers within a member function
  • quiver not drawing arrows just lots of blue, matlab
  • ASP.NET MVC Application won't update some controllers
  • Multicolored edittext hint
  • Authentication in Play! and RestEasy
  • Redux Form - Not able to type anything in input
  • Unable to install Git-core+svn by MacPorts
  • Unable to decode certificate at client new X509Certificate2()
  • jQuery ready not fired after rails link_to is clicked
  • How can I sort a a table with VBA with given text condition?
  • preg_replace Double Spaces to tab (\\t) at the beginning of a line
  • Bad request using file_get_contents for PUT request in PHP
  • Extracting HTML between tags
  • The plugin 'org.apache.maven.plugins:maven-jboss-as-plugin' does not exist or no valid ver
  • MongoDB in PHP using aggregate to group by _id is null not working
  • All Classes Conforming to Protocol Inherit Default Implementation
  • PHPUnit_Framework_TestCase class is not available. Fix… - Makegood , Eclipse
  • Launch Runnable Jar from Web Start
  • Why HTML5 Canvas with a larger size stretch a drawn line?
  • How do I use the BLAS library provided by MATLAB?
  • Rearranging Cells in UITableView Bug & Saving Changes
  • Matrix multiplication with MKL
  • Proper way to use connect-multiparty with express.js?
  • Understanding cpu registers
  • How do I configure my settings file to work with unit tests?
  • Memory offsets in inline assembly
  • How can I remove ASP.NET Designer.cs files?
  • python draw pie shapes with colour filled
  • Is there any way to bind data to data.frame by some index?
  • How can i traverse a binary tree from right to left in java?
  • Conditional In-Line CSS for IE and Others?
  • How do I use LINQ to get all the Items that have a particular SubItem?