Nested ifs, while, for, for not working
Mostrar comentarios más antiguos
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 comentarios
Bhavanithya Thiraviaraja
el 16 de Mayo de 2018
What are COCell and tempF?
Onur Sümbül
el 16 de Mayo de 2018
Onur Sümbül
el 21 de Mayo de 2018
Respuestas (1)
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 comentarios
Onur Sümbül
el 21 de Mayo de 2018
Onur Sümbül
el 21 de Mayo de 2018
Editada: Guillaume
el 21 de Mayo de 2018
Guillaume
el 21 de Mayo de 2018
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
el 21 de Mayo de 2018
Onur Sümbül
el 21 de Mayo de 2018
Editada: Onur Sümbül
el 21 de Mayo de 2018
Guillaume
el 21 de Mayo de 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
el 21 de Mayo de 2018
Editada: Onur Sümbül
el 21 de Mayo de 2018
Guillaume
el 21 de Mayo de 2018
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
el 21 de Mayo de 2018
Onur Sümbül
el 22 de Mayo de 2018
Editada: Onur Sümbül
el 22 de Mayo de 2018
Guillaume
el 22 de Mayo de 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.
Categorías
Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!