call on rows in order from arrays of different sizes

조회 수: 1 (최근 30일)
john gutsch
john gutsch 2018년 2월 1일
댓글: john gutsch 2018년 2월 2일
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.

채택된 답변

Jos (10584)
Jos (10584) 2018년 2월 1일
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
john gutsch
john gutsch 2018년 2월 2일
That was exactly what I needed, thanks for the great answer.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by