0

I was wondering what is the best way to group different matrices together.

I have multiple matrices called A1 (of size 9 x 48), A2 (of size 3 x 48), A3, A4 and so on. Each of these matrices are of a different size.

I want to group all of these together so that I can easily call them in another function that iterates through each matrix. Something like: Group = {A1, A2, A3, A4}

Therefore, I can run a function which calls each matrix and runs a test on it then goes to the next.

Would the best way to do this be a cell array?

rayryeng
  • 102,964
  • 22
  • 184
  • 193
  • 4
    Yes I would say that cell arrays are the way to go. After that, use `cellfun` so you can process each cell and apply your test. – rayryeng May 13 '14 at 03:47
  • How would you go about putting the matrices in a cellarray if its an undetermined amount? – user3575908 May 13 '14 at 03:48
  • That's pretty simple. Start with an empty cell array like `A = {};`. After, simply concatenate with as many matrices as you want. You can do something like this as an example: `A = [A rand(2)]; A = [A rand(3) rand(5)];` This performs two separate concatenate operations. After the end of these two, you will have a three element cell array of random matrices. Just keep appending via `A = [A ...]` until you want to stop. – rayryeng May 13 '14 at 03:52
  • 1
    Try this - `A = [{A1},{A2},{A3},{A4}]; out = cellfun(@your_function_name ,A,'uni',0);`. Please note that you need to put the other input parameters to the function through `cellfun` and also that `out` would be a cell array though. If `cellfun` looks like a tedious task, you can use a for-loop rather like this - `for k = 1:numel(A),out = your_function_name(A{k},param2,param3);end`. – Divakar May 13 '14 at 04:09
  • 1
    You can also grow a cell matrix like this: `Group(end+1) = An` assuming that `Group` is already a cell array – Dan May 13 '14 at 07:26

0 Answers0