Clear Filters
Clear Filters

Erase data assigned to the variables for every iteration of the FOR LOOP

2 views (last 30 days)
Hi,
I have 6 table data with each of size 2000x100 with a random 1 & 0. I would like to sum all these 6 table data using a for loop and if the sum is less than 100 then i skip the particular case. If the sum is equal to 100 then i write that specific case to a new variable.
Now the issue is the Variable to combine and sum these 6 table data does not start at first row during each iteration. Instead they get shifted down by 1 row everytime.
I have pasted a sample code below, someone please help me fix it.
Sample Code: (I am just displaying 2 table variables)
K = 20
for i = 1:1:K
x1(i,:) = a1{i,:};
x2(i,:) = a2{i,:};
Threshold = [x1; x2];
Sum = sum(Threshold,2)';
clear Threshold
if Sum == 26
y1(i,:) = x1{i,:};
y2(i,:) = x2{i,:};
clear Sum
else
i = i +1;
clear Sum
end
clear x1 x2
end
  2 Comments
Guillaume
Guillaume on 10 Jan 2020
I'm sorry to say that your code doesn't make sense. The indexing of x1 and x2 appears to be completely pointless and part of your code can't possibly work (the if and the mix-up between () and {} indexing).
Also, it's not clear what you are using tables. It looks like your ai variables should be matrices. You are not using any features of tables. It also appears that you're usin numbered variables a1 to a6. Numbered variables are always a bad idea. If you'd stored your 6 tables/matrices in a cell array, trivial indexing would have made your job dead easy.
Am I correct in understand that you want to sum your 6 tables across the columns and keep only those rows of the tables for which the sum is equal to 100?
Raghu Vamsi
Raghu Vamsi on 10 Jan 2020
Edited: Raghu Vamsi on 10 Jan 2020
I am new to coding and arrays are totally new. As i said i have 6 table data of size 2000x100 just like the attached mat file with two variables a1 & a2 each of size 20x26 with only 1'2 & 0's.
So i want to add the data along the row which means if the complete row is only 1's then the sum would be 26 which means that the row is successful and then in that case i want to store that data in a1 & a2 to a new variable say y1 & y2.
I am assigning a1 and a2 to x1 & X2 one row at a time and move them Threshold which would now have 2 rows. Now i perform a sum on both the rows of threshold and both the rows should satisfy the condition which is sum = 26, only then y1 and y2 are assigned the row from x1 & x2 respectively.
If the condition is not satisfied then the data is not stored to y1 & y2 and it is skipped to the next iteration. Once the next iteration starts i want to start Threshold and Sum to start fresh which means data for i = 2 until n should start to stored from row 1 rather than what the code is doing now.
My reply above is my requirement with respect to the sample code that i have attached. I hope i was clear if not let me know, i will try to make it even simpler if possible, hope i dont confuse you.
Just a small update, now i am able to satify what i need, but unable to assign x1 & x2 to y1 & y2 respectively. can you help me fix with the new code.

Sign in to comment.

Accepted Answer

Guillaume
Guillaume on 10 Jan 2020
First, let's fix your starting point. As I said, for what you're doing tables are the wrong storage. They're more complicated to use and use more memory. Ideally, you'd go back to whichever code created your a tables so that a) they're not numbered and b) they're matrices. So instead of tables, we're going to use matrices. Since your matrices are 2D and have all the same size, the simplest is to store them all in a single 3D matrices:
allmatrices = cat(3, table2array(a1), table2array(a2), table2array(a3), table2array(a4), table2array(a5), table2array(a6));
Numbered variables forces you to repeat the same code many times, as you can see above. Always a bad idea...
I'm still unclear of your goal.
If you want to keep only the rows that are all ones, it's simply:
selectedrows = allmatrices(all(allmatrices == 1, [2 3]), :, :); %keep rows that are all 1 across columns and matrices. This syntax requires R2018b or later
which will output a ?x100x6 matrix with only the rows containing all ones.
If you want to keep only the rows that sum to 100, it's simply:
selectedrows = allmatrices(sum(allmatrices, [2 3]) = 100, :, :); %keep only the rows that sum to 100 across the columns and matrices. Syntax requires R2018b or later
  5 Comments
Raghu Vamsi
Raghu Vamsi on 10 Jan 2020
Edited: Raghu Vamsi on 10 Jan 2020
Hi Guilaume,
I used the OPtion 1 above and it satisfies my requirement. Just have another question, allmatrices has both a1 and a2 values. Once i have used the Option1 how do i split and save the results in selectedrows to say maybe x1 & x2
And what does 3 in the command exactly do????
Guillaume
Guillaume on 10 Jan 2020
how do i split and save the results in selectedrows to say maybe x1 & x2
You don't. As I said, don't use numbered variable. Wherever you were going to write x1, write selectedrows(:, :, 1). Wherever you were going to write x2, write selectedrows(:, :, 2).
As I wrote as a comment, numbered variables are going to force you to write the same code for each variable whereas keeping it all together in one variable, you only have to write it once.
what does 3 in the command do
all(allmatrices == 1, 2)
transforms a MxNxP matrix into a Mx1xP matrix which is 1 where all the elements of the (M, :, P) row (i.e. all the elements across the 2nd dimension, hence the 2) are 1.
all(.., 3)
then transforms than Mx1xP matrix into a Mx1x1 matrix which is 1 where all the elements across the 3rd dimension are 1.

Sign in to comment.

More Answers (0)

Categories

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

Tags

Community Treasure Hunt

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

Start Hunting!