I have a matrix and let's say what's in the matrix represents people. I want to find the expected column value of a person in the matrix, ignoring the people in the latest columns that exceed the number of people in any other row.
This is the code I have now:
lastToInclude = min(XOrowSummedDomMatrix);
XOweightedSum = zeros(numel(COCell),1);
mySum = zeros(numel(COCell),1);
for kk1=1:numel(COCell)
for kk2=1:numel(tempF)
while (mySum(kk1) < lastToInclude)
if (mySum(kk1) + XOsummedDomMatrix(kk1,kk2) < lastToInclude)
XOweightedSum(kk1) = XOweightedSum(kk1) + (kk2 * XOsummedDomMatrix(kk1,kk2));
mySum(kk1) = mySum(kk1) + XOsummedDomMatrix(kk1,kk2);
end
if (mySum(kk1) + XOsummedDomMatrix(kk1,kk2) == lastToInclude)
XOweightedSum(kk1) = XOweightedSum(kk1) + (kk2 * XOsummedDomMatrix(kk1,kk2));
mySum(kk1) = mySum(kk1) + XOsummedDomMatrix(kk1,kk2);
end
if (mySum(kk1) + XOsummedDomMatrix(kk1,kk2) > lastToInclude)
XOweightedSum(kk1) = XOweightedSum(kk1) + ((lastToInclude - mySum(kk1)) * XOsummedDomMatrix(kk1,kk2));
mySum(kk1) = lastToInclude;
end
end
end
end
XOexpFront = XOweightedSum / lastToInclude;
Everything works right until the for loop starts as far as I can tell. Here, XOsummedDomMatrix is the matrix that I'm talking about, say lastToInclude is the minimum number of people in the rows of XOsummedDomMatrix, kk1 and kk2 represent the dimensions of XOsummedDomMatrix correctly. After I enter this code to the command window after the preleminaries are calculated correctly, it seems to just pause there until I stop it, and it's like it never entered the for loop at all because XOweightedSum still returns all zeros.
Please help

3 commentaires

What are COCell and tempF?
Onur Sümbül
Onur Sümbül le 16 Mai 2018
COCell is the essence of the rows of XOrowSummedDomMatrix, XOsummedDomMatrix, XOweightedSum. We can think of it like categories of people.
tempF is the essence of the columns of XOsummedDomMatrix. We can think of it like the ranks of people.
Onur Sümbül
Onur Sümbül le 21 Mai 2018
Actually it is part of a method to evaluate more than one multi-objective algorithms that I want to propose, and I still couldn't solve the problem.

Connectez-vous pour commenter.

Réponses (1)

Guillaume
Guillaume le 21 Mai 2018
Modifié(e) : Guillaume le 21 Mai 2018
Rather than giving us code that doesn't work and hence we can't understand, give us a numeric example of input and desired output (with an explanation of how said output is obtained). That would be a lot more useful.
You're saying that XOrowSummedDomMatrix is a matrix, which usually means a 2D array. If that is the case, then min(XOrowSummedDomMatrix) is a vector which means that lastToInclude is a vector.
You then have
while something < lastToInclude
the while will only be true if something is strictly smaller than all the elements of lastToInclude. I suspect that's not what you intended but if it was, you should make that explicit:
while all(something < lastToInclude)
Same for the ifs, the comparisons must be true for all the elements of lastToIndex for an if to be true. So if just one element of lastToInclude is smaller than your sum while all the others are larger, then none of the if will be true, nothing will be changed, you'll go back to the while which will indeed loop indefinitively.
I suspect that loops and ifs are not needed, but as said, give us an example of what you want so we can tell for sure.

11 commentaires

Onur Sümbül
Onur Sümbül le 21 Mai 2018
XOrowSummedDomMatrix is one dimensional, it was a naming convenience for me. lastToInclude contains a single number, I checked it and it was 7 hundred and sth.
Onur Sümbül
Onur Sümbül le 21 Mai 2018
Modifié(e) : Guillaume le 21 Mai 2018
Let's say I will be given matrices(XOsummedDomMatrix) of unknown contents and I assume the numbers represent quantity of sth, and I want to find the expected column of sth in each of those matrices but including only the minimum number of things in any of the rows, starting from the left-most column.
Now, say I'm given this matrix and I call it XOsummedDomMatrix:
1, 0, 2, 4, 1
2, 1, 1, 0, 1
0, 1, 1, 1, 4
XOrowSummedDomMatrix is then this:
8
5
7
and lastToInclude is then 5
Now I look at the XOsummedDomMatrix again, and I want Matlab to calculate this or equivalent for me:
1*1 + 0*2 + 2*3 + 2*4
2*1 + 1*2 + 1*3 + 0*4 + 1*5
0*1 + 1*2 + 1*3 + 1*4 + 2*5
Note that the first and third rows are not directly multiplied by the column numbers cell by cell because they have extra things compared to row two.
That's the part that does not work and in this example it must give:
1+6+8 = 15
2+2+3+5 = 13
2+3+4+10 = 19
Then it would divide it to lastToInclude, which is 5 in this example, and give:
15/5 = 3
13/5 = 2.6
19/5 = 3.8
Then looking at these numbers I could tell which of the algorithms can be expected to give higher quality solutions and which can be expected to give lower quality solutions.
In
1*1 + 0*2 + 2*3 + 2*4
2*1 + 1*2 + 1*3 + 0*4 + 1*5
0*1 + 1*2 + 1*3 + 1*4 + 2*5
Why is the 1 at the end of the first row not included?
Why is the 4 at the end of the last row changed to a 2?
I understand that row 2 is just a plain sum(value.*columnnumber) because it is the column with the lowest sum but I don't understand what rules governs the choice of missing/replaced elements in the other row.
Also, what if several rows have the same minimum sum?
Onur Sümbül
Onur Sümbül le 21 Mai 2018
Because row 2 has 5 things, but row 1 has 8 and row 3 has 7 things. So I kind of subtract 3 from the end of the first row and 2 from the end of the third row.
Onur Sümbül
Onur Sümbül le 21 Mai 2018
Modifié(e) : Onur Sümbül le 21 Mai 2018
So, if the total number of things in rows in that matrix were 6, 7, 7, 6, 5, 5, the last 1, 2, 2, and 1 thing would be excluded from the calculation in the first 4 rows respectively, and the last two rows would be taken to calculation as they are.
Guillaume
Guillaume le 21 Mai 2018
I'd edited your comment to make it read right, and you went and edited it again to mess it up!
Now, I understand. If the sum of a row is so much greater than the minimum that subtracting the difference results in a negative value for the last column. What happens? It goes negative or the overflow spills in the previous column(s)?
Onur Sümbül
Onur Sümbül le 21 Mai 2018
Modifié(e) : Onur Sümbül le 21 Mai 2018
Overflow spills. I didn't even realize I edited sth you corrected.
I'm not sure it's possible to process the overflow without a loop over the columns, but all the rest can be done easily with matrix operations:
XOsummedDomMatrix = [1, 0, 2, 4, 1
2, 1, 1, 0, 1
0, 1, 1, 1, 4];
XOrowSummedDomMatrix = sum(XOsummedDomMatrix, 2);
lastToInclude = min(XOrowSummedDomMatrix);
tospread = XOrowSummedDomMatrix - lastToInclude;
col = size(XOsummedDomMatrix, 2);
while any(tospread)
tosubtract = min(tospread, XOsummedDomMatrix(:, col)); %values to remove from current column
XOsummedDomMatrix(:, col) = XOsummedDomMatrix(:, col) - tosubtract;
tospread = tospread - tosubtract;
col = col - 1;
end
XOweightedSum = sum(XOsummedDomMatrix .* (1:size(XOsummedDomMatrix, 2)), 2);
XOexpFront = XOweightedSum / lasttoInclude;
Onur Sümbül
Onur Sümbül le 21 Mai 2018
Yep, I guess creating a new matrix with excess members removed first worked. I now think the problem might be about nested if and while not working in Matlab.
Onur Sümbül
Onur Sümbül le 22 Mai 2018
Modifié(e) : Onur Sümbül le 22 Mai 2018
Hey I just found the problem, if XOsummedDomMatrix(kk1,kk2)=0 the code I first tried enters an endless loop :-( Thanks for the help
Guillaume
Guillaume le 22 Mai 2018
Note that the code I've provided is a lot more efficient than yours, particularly for larger matrices. Mine only loop over a few column rather than the whole matrix.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Produits

Version

R2017a

Commenté :

le 22 Mai 2018

Community Treasure Hunt

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

Start Hunting!

Translated by