For loop indexing problem

4 ビュー (過去 30 日間)
Francis Chabot
Francis Chabot 2021 年 4 月 1 日
コメント済み: Francis Chabot 2021 年 4 月 1 日
Hello,
I'm trying to use a for loop to be able to create a new matrix much more quickly. The loop is :
for ii=1:Nco;
for jj=1:Nyrs
BMactive(:,jj) = [idact(:), BMact(:,jj)];
end
end
Where Nco correspond to the numbers of company which is 749, Nyrs correspond to the numbers of years which is 20, idact is a 749x1 matrix and BMact is a 749x20 matrix.
What I'm trying to do is to create 20 matrix of 749x2 which are form of idact and each colon of BMact and I can't figure out a way to do so.
Best regards

採用された回答

Bob Thompson
Bob Thompson 2021 年 4 月 1 日
編集済み: Bob Thompson 2021 年 4 月 1 日
Part of the problem is that you're trying to push a 749x2 matrix into an array of 749x1 size (BMactive(:,jj) only calls one column at a time).
As for creating 20 matrices, do you really need 20 matrices, or do you just want 20 sets of the information? Creating a number of matrices is generally not recommended with MATLAB, instead we generally recommend you index to another dimension. See the following as an example:
for ii=1:Nco; % Is there more content within the loop? Because this doesn't actually do anything with
% what you've posted
for jj=1:Nyrs
BMactive(:,:,jj) = [idact(:), BMact(:,jj)]; % Adjusted BMactive indexing
end
end
The simple change implemented should change BMactive to be the size of 749x2x20, where each 'sheet' is your 749x2 array that you wanted to put into a separate array. If you need to call it specifically, just index to the desired sheet.
If this does not address your problem at all, please explain more, because I clearly don't understand.
  1 件のコメント
Francis Chabot
Francis Chabot 2021 年 4 月 1 日
I was thinking to do it in 20 matrix but with this loop I can wasily extract them after so it worked like a charm! And yes I forgot to remove the for ii.
Thank you

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by