Storing index logic in new column per iteration

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

Answers (1)

Timothy
Timothy on 2 Nov 2021
Edited: Timothy on 2 Nov 2021
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 Comments

I get the following error when I try your suggestion:
"Unable to perform assignment because the size of the left side is 1-by-1 and the size of the right side is 274-by-1."
Which has been the error that I have been struggling with
On which line do you get this error?
data.idx2(i,j) = ismember(firstoutput(:,2),secondoutput(:,2));
here is what I wrote from your suggestions
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
Ah, OK, I see what I missed now that I'm looking at it again. The output of is member is different than what I was initially thinking. You just need another dimension on your data.idx# fields to accomodate the output of ismember. Is the length of data.variables_## fixed or variable? If it is fixed, you can add the third dimension size to the zeros call to pre-allocate your data.idx# fields. If it is variable, then you may want to make your data.idx# fields into any array of cells instead.
For cell arrays:
Preallocate using ... = cell(L-1);
Access using {} instead of () for data.idx#, like data.idx3{i,j} = ...
You're welcome, were you able to get something working the way you wanted?
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

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Release

R2021b

Community Treasure Hunt

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

Start Hunting!