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

조회 수: 6 (최근 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 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