Combining 2 two-dimensional cell arrays in MATLAB

In summary, the user wants to create a new cell that will contain the data from the two original cells. They want the data in one column and want to do this using a for loop.
  • #1
chendry2001
7
0
Hi

I'm quite new to MATLAB and I'm stuck on a problem which I think is probably relatively simple. Any help much appreciated!

Right, I have 2 cells, both of dimension 51x1 called n_cbc and n_load. Each of the 51 rows in the n_cbc cell contains a 5x1 matrix. Each of the 51 rows in the n_load cell contains a 3x1 matrix.

What I want to do is create a new 3rd cell that contains the data from these 2 cells. It should also be of dimension 51x1 and each row should now have a 8x1 matrix (the first 5 values from n_cbc and the next 3 values from n_load).

I always end up with a 51x2 cell, but I want all values in the one column.

Hope that made sense. Any ideas?

Thanks!
 
Physics news on Phys.org
  • #2
chendry2001 said:
Hi

I'm quite new to MATLAB and I'm stuck on a problem which I think is probably relatively simple. Any help much appreciated!

Right, I have 2 cells, both of dimension 51x1 called n_cbc and n_load. Each of the 51 rows in the n_cbc cell contains a 5x1 matrix. Each of the 51 rows in the n_load cell contains a 3x1 matrix.

What I want to do is create a new 3rd cell that contains the data from these 2 cells. It should also be of dimension 51x1 and each row should now have a 8x1 matrix (the first 5 values from n_cbc and the next 3 values from n_load).

I always end up with a 51x2 cell, but I want all values in the one column.

Hope that made sense. Any ideas?

Thanks!

I'd suggest using a 'for' loop (you probably want to do this in an m-file). In it, you'd iterate 51 times and within each iteration, you'd concatenate the two vectors (e.g. c = [a, b];)

For loop:
http://www.mathworks.com/access/helpdesk/help/techdoc/ref/for.html

M-files (in case you didn't know):
http://www.mathworks.com/access/helpdesk/help/techdoc/learn_matlab/f4-2525.html#f4-2543
 
Last edited by a moderator:
  • #3
ok, thanks. it worked using this

for i = 1:51
[nodes{i}] = [n_cbc{i};n_load{i}];
end
 

1. How do I combine two two-dimensional cell arrays in MATLAB?

To combine two two-dimensional cell arrays in MATLAB, you can use the "cat" function. This function allows you to concatenate the two arrays along a specified dimension, such as rows or columns. Alternatively, you can use the "vertcat" or "horzcat" functions to concatenate the arrays vertically or horizontally, respectively.

2. Can I combine two two-dimensional cell arrays with different sizes in MATLAB?

Yes, you can combine two two-dimensional cell arrays with different sizes in MATLAB. However, the resulting array will have the size of the larger array, and the elements of the smaller array will be padded with empty cells. Alternatively, you can use the "resize" function to adjust the size of the arrays before combining them.

3. How do I merge two two-dimensional cell arrays with overlapping data in MATLAB?

To merge two two-dimensional cell arrays with overlapping data in MATLAB, you can use the "union" function. This function combines the unique elements from both arrays while preserving the order. You can also use the "unique" function to remove any duplicate elements before merging the arrays.

4. Can I combine two two-dimensional cell arrays with different data types in MATLAB?

Yes, you can combine two two-dimensional cell arrays with different data types in MATLAB. However, the resulting array will have the data type of the first array, and the elements of the second array will be converted to match the data type. If the data types are not compatible, an error will occur. You can use the "convertCharsToStrings" function to convert character arrays to string arrays before combining them.

5. Is there a limit to the number of two-dimensional cell arrays that can be combined in MATLAB?

No, there is no limit to the number of two-dimensional cell arrays that can be combined in MATLAB. As long as the arrays have the same number of dimensions, they can be combined using the functions mentioned above. However, keep in mind that combining a large number of arrays can affect the performance of your code.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
32
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
18
Views
5K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
Replies
1
Views
718
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
Back
Top