Storing index logic in new column per iteration

조회 수: 1 (최근 30일)
Annette Ciecierega
Annette Ciecierega 2021년 11월 2일
댓글: Annette Ciecierega 2021년 11월 3일
For each idx variable, I want the values to be stored in a new column per iteration. Right now, each idx is being overwritten per loop iteration. I do not want the idx logic to be stored in one column with new rows; each iteration should make a new column in the idx logic field.
for j = 2:length(fieldnames(data))
for i = 1:(length(fieldnames(data))-1)
firstoutput = data.(sprintf("variables_%d", i));
secondoutput = data.(sprintf("variables_%d",j));
data.idx2 = ismember(firstoutput(:,2),secondoutput(:,2));
data.idx3 = ismember(firstoutput(:,3),secondoutput(:,3));
data.idx4 = ismember(firstoutput(:,4),secondoutput(:,4));
end
end

답변 (1개)

Timothy
Timothy 2021년 11월 2일
편집: Timothy 2021년 11월 2일
You would need to use i and/or j, depending on how you are thinking of an "iteration" for your algorithm, as indices into your data.idx# fields. If i and/or j are large, I would also recommend pre-allocating them as a logical/boolean vector/matrix, as appropriate for your definition of iteration.
For example, it seems like you would want to store all combinations of i and j, based on your code so you might do something like this:
L = length(fieldnames(data));
data.idx2 = zeros(L-1, L-1, 'logical');
data.idx3 = data.idx2;
data.idx4 = data.idx2;
for j = 2:L
for i = 1:L-1
firstoutput = data.(sprintf("variables_%d", i));
secondoutput = data.(sprintf("variables_%d",j));
data.idx2(i,j) = ismember(firstoutput(:,2),secondoutput(:,2));
data.idx3(i,j) = ismember(firstoutput(:,3),secondoutput(:,3));
data.idx4(i,j) = ismember(firstoutput(:,4),secondoutput(:,4));
end
end
  댓글 수: 7
Timothy
Timothy 2021년 11월 3일
You're welcome, were you able to get something working the way you wanted?
Annette Ciecierega
Annette Ciecierega 2021년 11월 3일
I actually posted another question:
https://www.mathworks.com/matlabcentral/answers/1578430-plotting-repeating-values-in-excel-files-vs-the-files-that-they-overlap-in

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by