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

3 次查看(过去 30 天)
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, :);

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by