How to create a new table array combining the values every n,th element from two different table arrays

1 次查看(过去 30 天)
I have two table arrays with a big number of rows and one column each. Each array has a different size. I would like to add the values from the second array into the first so that every n values of A the same number of values of B are added to create a new table array C.
For example:
Given a matrix A=[2,5,7,9,11,13,15,17,19,21] and a matrix B= [1,1,1,1,1,1,3,3,3,3,3,3,4,4,4,4,4,6,6,6,6,6,6]
I would like to create a new matrix C = [2,5,1,1,1,1,1,7,9,3,3,3,3,3,3,11,13,4,4,4,4,4,15,17,6,6,6,6,6,6,19,21]
In this case every 2 values of A, 5 consecutive values from B are added.
In my case I would like to add every 60 values of A to add 59 consecutive values of B. The length of A is 2008x60= 120480 rows and B is 2008x59= 118472 rows.
  2 个评论
Guillaume
Guillaume 2019-10-16
Your example has alternatively 5 or 6 values from B added. That doesn't match your description.
Note that tables and arrays are two different things.
Danae Parissi
Danae Parissi 2019-10-16
编辑:Danae Parissi 2019-10-16
I am sorry for the typo, the number of values that are added from B are contantly 49.
Also they are all table arrays.

请先登录,再进行评论。

采纳的回答

Guillaume
Guillaume 2019-10-16
In my case I would like to add every 60 values of A to add 59 consecutive values of B
So, assuming that A and B are indeed vectors
C = reshape([reshape(A, 59, []); reshape(B, 60, [])], 1, [])
Basically, reshape A in columns of 59 rows, reshape B in columns of 60 rows, concatenate the two matrices vertically and reshape back into a vector.
  2 个评论
Danae Parissi
Danae Parissi 2019-10-16
编辑:Danae Parissi 2019-10-16
Thank you very much it works. Would you also know how to add 3 table arrays in a similar fashion?
I am simply doing what you showed me in sequence but I was wondering if there were something more elegant.
Guillaume
Guillaume 2019-10-16
Again, a table array is not a thing. A table is a completely different thing from an array.
If the 3 arrays to interlace are separate variables, it's the same principle:
reshape([reshape(A, Astride, []); ... where Astride is 59 in your example
reshape(B, Bstride, []); ... where Bstride is 60 in your example
reshape(C, Cstride, [])], ...
1, [])
If the arrays are stored in a cell array instead, there are simpler ways.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by