Combining 2 two-dimensional cell arrays in MATLAB

  • Context: MATLAB 
  • Thread starter Thread starter chendry2001
  • Start date Start date
  • Tags Tags
    Arrays Cell Matlab
Click For Summary
SUMMARY

The discussion focuses on combining two cell arrays in MATLAB, specifically two 51x1 cells named n_cbc and n_load. Each cell contains matrices of different dimensions: n_cbc has 5x1 matrices while n_load has 3x1 matrices. The goal is to create a new cell array that maintains the 51x1 structure but contains 8x1 matrices by concatenating the corresponding matrices from n_cbc and n_load. The solution involves using a for loop to iterate through the indices and concatenate the matrices.

PREREQUISITES
  • Understanding of MATLAB cell arrays
  • Familiarity with MATLAB for loops
  • Basic knowledge of matrix concatenation in MATLAB
  • Experience with MATLAB m-files
NEXT STEPS
  • Learn more about MATLAB cell array manipulation
  • Explore MATLAB matrix concatenation techniques
  • Study MATLAB for loop syntax and usage
  • Investigate best practices for writing MATLAB m-files
USEFUL FOR

Beginner MATLAB users, data analysts, and anyone looking to manipulate and combine cell arrays in MATLAB efficiently.

chendry2001
Messages
7
Reaction score
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
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:
ok, thanks. it worked using this

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

Similar threads

  • · Replies 9 ·
Replies
9
Views
5K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 32 ·
2
Replies
32
Views
4K
Replies
1
Views
3K
  • · Replies 18 ·
Replies
18
Views
6K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 10 ·
Replies
10
Views
4K
  • · Replies 2 ·
Replies
2
Views
5K