Info
Cette question est clôturée. Rouvrir pour modifier ou répondre.
While loop question in row addition sums. Want row to be less then 2.
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I am generating a matrix such as this,
1.0739 0.9517 0.1078 0.4077 0.7066 0.7983
0.6003 1.1885 0.1747 0.3946 0.3541 0.0705
0.7753 0.0929 0.0649 0.0188 0.0298 0.0954
0.4378 0.1140 2.8074 0.5060 0.7248 0.2897
0.1666 0.4086 0.3344 0.0417 0.5798 2.7909
0.0513 1.0805 0.4291 1.3061 0.1078 0.6336
0.5828 0.4903 2.0129 0.5368 0.2691 0.6476
0.1921 0.0626 0.1121 0.1333 0.0121 0.0682
0.2640 0.1722 0.3182 0.2676 1.0384 0.5087
0.5586 0.1019 0.1335 0.4800 0.5046 1.1357
0.2461 0.0407 0.2132 0.7918 0.1956 1.2577
1.4788 0.0586 0.3212 1.1473 0.3263 0.2079
0.2761 2.1830 0.6020 0.5124 0.0489 0.0253
0.0572 0.1084 0.3595 0.1271 0.6155 0.0778
0.2312 0.1831 0.0423 0.1079 0.6713 0.1707
0.2744 0.6776 0.3923 1.0708 0.1322 0.1165
I also have a matrix the same size full of zeros. I want to loop trough every row and sum every value until the next value yields a value above 2.
So a row such as [1.0739 0.9517 0.1078 0.4077 0.7066 0.7983 ...] would loop trough every value and if the sum is not 2 yet, it would rewrite it in the other matrix that is now zeros.
initiate_matrix_z = zeros(16,6);
initiate_matrix_p = %the numbered matrix
for r=1:16 %rows
while (sum <= 2) %note that all sums must be below 2
for c=1:6 %for all values in row
sum = initiate_matrix_p(r,c) + sum %take the value and add it to previous sum
end
end
At the end of this I want the following:
1.0739 0 0 0 0 0 %second value makes sum of row exceed 2
0.6003 1.1885 0.1747 0 0 0 %fourth value makes sum of row exceed 2
0.7753 0.0929 0.0649 0.0188 0.0298 0.0954 %no value makes sum(row)>2
0.4378 0.1140 0 0 0 0 %third value makes sum of row exceed 2
2 commentaires
Image Analyst
le 9 Avr 2017
DON'T USE sum AS THE NAME OF YOUR VARIABLE BECAUSE IT'S THE NAME OF A VERY IMPORTANT BUILT IN VARIABLE. Now, do you NEED to use a loop, or can you use cumsum() like the vast majority of good MATLAB programmers would use?
Réponses (2)
Image Analyst
le 9 Avr 2017
Try this:
m = [...
1.0739 0.9517 0.1078 0.4077 0.7066 0.7983
0.6003 1.1885 0.1747 0.3946 0.3541 0.0705
0.7753 0.0929 0.0649 0.0188 0.0298 0.0954
0.4378 0.1140 2.8074 0.5060 0.7248 0.2897
0.1666 0.4086 0.3344 0.0417 0.5798 2.7909
0.0513 1.0805 0.4291 1.3061 0.1078 0.6336
0.5828 0.4903 2.0129 0.5368 0.2691 0.6476
0.1921 0.0626 0.1121 0.1333 0.0121 0.0682
0.2640 0.1722 0.3182 0.2676 1.0384 0.5087
0.5586 0.1019 0.1335 0.4800 0.5046 1.1357
0.2461 0.0407 0.2132 0.7918 0.1956 1.2577
1.4788 0.0586 0.3212 1.1473 0.3263 0.2079
0.2761 2.1830 0.6020 0.5124 0.0489 0.0253
0.0572 0.1084 0.3595 0.1271 0.6155 0.0778
0.2312 0.1831 0.0423 0.1079 0.6713 0.1707
0.2744 0.6776 0.3923 1.0708 0.1322 0.1165]
[rows, columns] = size(m);
output = zeros(size(m));
for row = 1 : rows
cdf = cumsum(m(row, :))
index = find(cdf < 2, 1, 'last');
cdf(index+1:end) = 0;
output(row, :) = cdf;
end
output % Print to command window
0 commentaires
Walter Roberson
le 9 Avr 2017
temp = cumsum( initiate_matrix_p, 2);
mask = temp < 2;
initiate_matrix_z(mask) = temp(mask);
No loop.
0 commentaires
Cette question est clôturée.
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!