How do I add values to an array on a conditional basis?

1 回表示 (過去 30 日間)
Cai Chin
Cai Chin 2020 年 12 月 25 日
コメント済み: Cai Chin 2020 年 12 月 26 日
Hi, I am trying to add values to a pre-allocated array based on whether the row number belongs to a set of values. In this example, I have generated an array with 7 rows and 900 columns, and only when the row number of another array matches the numbers in a specified vector, do I want the column values from that array to be added to my new array. At the moment, it keeps changing the dimension of the array so I am left with zeros in several of the unfilled positions. Any suggestions would be greatly appreciated, thanks in advance.
abnormal_cycles_5 = [95, 94, 92, 91, 79, 72, 1];
values_v_5_abnormal = zeros(length(abnormal_cycles_5), 900);
values_w_5_abnormal = zeros(length(abnormal_cycles_5), 900);
for i = 1:95
if ismember(i, abnormal_cycles_5)
values_v_5_abnormal(i, 1:900) = values_v_5(i, 1:900);
values_w_5_abnormal(i, 1:900) = values_w_5(i, 1:900);
end
end
I have attached the variable values 'values_v_5' and 'values_w_5':

採用された回答

Image Analyst
Image Analyst 2020 年 12 月 25 日
Do you mean like this:
abnormal_cycles_5 = [95, 94, 92, 91, 79, 72, 1]; % 7 columns, 1 row
values_v_5_abnormal = zeros(length(abnormal_cycles_5), 900); % 7 rows, 900 columns.
values_w_5_abnormal = zeros(length(abnormal_cycles_5), 900); % 7 rows, 900 columns.
values_w_5_abnormal(:, abnormal_cycles_5) = repmat(abnormal_cycles_5', 1, 7);
where the 7 element vector "abnormal_cycles_5" gets loaded into columns 95, 94, 92, 91, 79, 72, and 1 of the 2-D matrix "values_w_5_abnormal"?
  5 件のコメント
Image Analyst
Image Analyst 2020 年 12 月 26 日
Do you mean like this:
v = load('values_v_5.mat')
values_v_5 = v.values_v_5;
w = load('values_w_5.mat')
values_w_5 = w.values_w_5;
abnormal_cycles_5 = [95, 94, 92, 91, 79, 72, 1]; % 7 columns, 1 row
values_v_5_abnormal = zeros(length(abnormal_cycles_5), 900); % 7 rows, 900 columns.
values_w_5_abnormal = zeros(length(abnormal_cycles_5), 900); % 7 rows, 900 columns.
values_v_5_abnormal = values_v_5(abnormal_cycles_5, :);
values_w_5_abnormal = values_w_5(abnormal_cycles_5, :);
Cai Chin
Cai Chin 2020 年 12 月 26 日
Yes, thank you very much!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by