Adding rows to matrix based on if-statement in loop

Hey,
I have a matrix with numerical values, however, some data is missing and I have to add some additional rows to the matrix.
The problem is that the rows have to added at a specific place of the matrix since the order of the matrix is important.
I would like to automate the process and work with a loop in which rows are added based on an if-statement which refers to specific values in the matrix
The order of the other rows of the matrix should remain the same.
Any ideas how to implement this?

1 件のコメント

Stephen23
Stephen23 2020 年 2 月 5 日
編集済み: Stephen23 2020 年 2 月 5 日
"Any ideas how to implement this?"
Why do you need a loop?
Preallocate the complete output matrix using nan or zeros, then use indexing to allocate all rows of data in one operation.

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

 採用された回答

Star Strider
Star Strider 2020 年 2 月 5 日

0 投票

A = randi(9, 7, 5); % Original Matrix
B = randi([51 59], 2, 5); % Rows To Be Inserted
Anew = zeros(size(A,1)+size(B,1), size(A,2)); % Output Matrix Preallocation
NewRows = [3 5]; % Define Rowss For Insertion
ARows = setdiff(1:size(Anew,1), NewRows); % Original Rows In New Matrix
Anew(ARows,:) = A; % Copy Rows
Anew(NewRows,:) = B; % Insert Rows

2 件のコメント

Kai Friedrich
Kai Friedrich 2020 年 6 月 16 日
Thanks a lot!
Star Strider
Star Strider 2020 年 6 月 16 日
As always, my pleasure!

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および 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