call on rows in order from arrays of different sizes

1 vue (au cours des 30 derniers jours)
john gutsch
john gutsch le 1 Fév 2018
Commenté : john gutsch le 2 Fév 2018
I have multiple arrays of different dimensions, and I want to send each row from each array as a separate UDP message. The rows in each array are already in the correct order, but I need to send them in an order across the array. For example: A = [1 0 0; 2 0 0; 4 0 0] and B = [3 0 0 0; 5 0 0 0; 6 0 0 0]. The first column is the order that the UDP needs to send at. So we send the first row from A, then the second row from A, then the first row from B, then the third row from A, etc...
I can't combine the arrays because they are different sizes. Any ideas on how to grab the individual rows in order?
I didn't include my code so far because the arrays are imported from CSVs, but I can post everything if that's helpful or my question is unclear.

Réponse acceptée

Jos (10584)
Jos (10584) le 1 Fév 2018
My idea:
  1. Convert A and B into cell arrays (each row becomes a cell)
  2. Merge into a single cell array
  3. Sort based on first value
  4. Send each cell
A = [1 10 ; 2 20 ; 4 40 ], B = [3 30 30 ; 5 50 50]
C = [mat2cell(A,ones(1,size(A,1)),size(A,2)) ; mat2cell(B,ones(1,size(B,1)),size(B,2))] % #1 & #2
[~,ix] = sort([A(:,1) ; B(:,1)]) % #3.1
C = C(ix) % #3.2
% #4
  1 commentaire
john gutsch
john gutsch le 2 Fév 2018
That was exactly what I needed, thanks for the great answer.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by