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

2 Ansichten (letzte 30 Tage)
Cai Chin
Cai Chin am 25 Dez. 2020
Kommentiert: Cai Chin am 26 Dez. 2020
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':

Akzeptierte Antwort

Image Analyst
Image Analyst am 25 Dez. 2020
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 Kommentare
Image Analyst
Image Analyst am 26 Dez. 2020
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 am 26 Dez. 2020
Yes, thank you very much!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by