How to insert rows into a matrix

2 ビュー (過去 30 日間)
Elaheh
Elaheh 2018 年 12 月 14 日
コメント済み: Mohsin Shah 2019 年 4 月 15 日
Hi
I have a matrix (748*6). I need to insert one row after every 3rd row. How should I do that? the resulting martix should be (1126 *6).
Thank you
  2 件のコメント
Jan
Jan 2018 年 12 月 14 日
編集済み: Jan 2018 年 12 月 14 日
This cannot work. "After every 3rd row" means, that the number of rows would grow by 4/3. Example with 6 elements:
[1,2,3,<insert>, 4,5,6,<insert>]
Maybe you mean "after every 2nd row" or "in every 3rd row". Then the number of rows grows by a factor of 3/2. This is near to the given sizes, but not exactly: 748 / 2 * 2 is 1122, not 1126.
Please post a short example with inputs and outputs to clarify, what you want.
You did not mention what you want to insert: zeros, NaNs, values from another matrix? "Inserting rows" is not clear in this point.
Elaheh
Elaheh 2018 年 12 月 14 日
apologize for not being clear. This is one part of data. Current table is on the right and the one I need to create is on the left. Capture.PNG

サインインしてコメントする。

採用された回答

Jan
Jan 2018 年 12 月 14 日
If the details with the size of the output are cleared, something like this will solve the problem:
A = rand(748, 6); % Test data
n = 3; % In each 3rd row
m = true(748 / (n-1) * n, 1);
m(n:n:end) = false;
B = zeros(numel(m), 6);
B(m, :) = A;
  1 件のコメント
Mohsin Shah
Mohsin Shah 2019 年 4 月 15 日
Awsome!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by