Effacer les filtres
Effacer les filtres

Negating every second entree in a matrix column

3 vues (au cours des 30 derniers jours)
Michael Dzjaparidze
Michael Dzjaparidze le 13 Déc 2011
My goal is pretty simple: I have an arbitrary sized matrix holding only positive real numbers and there will not be anymore than two nonzero elements per column at any time, for example:
b = [1 0 1; 0 1.2 2; 1 3 0];
or
b = [1 2 1 0; 0 1.2 0 0; 0 0 0 3; 1.6 0 4 2.2];
Now what I want is to negate every second nonzero entree of a column. The solution I came up with works, but is probably not the most elegant and/or efficient:
for n=1:numel(b(1,:))
isPositive = 0;
for m=1:numel(b(:,n))
if (isPositive == 1 && b(m,n) ~= 0)
b(m,n) = b(m,n)*-1;
break;
end
if (abs(b(m,n)) == b(m,n) && b(m,n) ~= 0)
isPositive = 1;
end
end
end
I am quite new to MATLAB, so if anybody knows a more elegant solution, perhaps not involving any for loops, please share.
Thanks in advance
  2 commentaires
Jan
Jan le 13 Déc 2011
The BREAK wil stop the "for m" loop - is this intented?
Instead of "(abs(b(m,n)) == b(m,n) && b(m,n) ~= 0)" you could write "b(m,n) > 0", but if the matrix b has positive values only at first, the test "abs(b(m,n)) == b(m,n)" is useless.
Why do you check if "b(m,n)~=0" before multiplying with -1? In Matlab "-0" is the same as "0".
Michael Dzjaparidze
Michael Dzjaparidze le 13 Déc 2011
Yes, the BREAK is intended, since as soon as the second item has been negated in a column, I don't need to check that column any further (i.e. mission accomplished for this particular column). This should save unnecessary iterations over 0's in case of large matrices, where the two nonzero elements are near the beginning of the column. I see your point with writing b(m,n) > 0 instead. Bit stupid I haven't noticed that myself. The reason I do the whole b(m,n) > 0 check is to flag when I come across the first nonzero, positive element in a column. I then know that the following nonzero, positive element in the same column needs to be negated and once this is done, I can go on to repeat this procedure for the next column. My rationale behind checking if b(m,n) ~= 0 before multiplying with -1 is purely because this saves an unnecessary multiplication operation, since like you say yourself: -0 == 0

Connectez-vous pour commenter.

Réponse acceptée

Andrei Bobrov
Andrei Bobrov le 13 Déc 2011
idx = find(b)
b(idx(2:2:end)) = - b(idx(2:2:end))
more variant
[i1 j1] = find(b)
[m m] = unique(j1,'first')
k = sub2ind(size(b),i1(m+1) ,j1(m+1))
b(k) = -b(k)
  7 commentaires
Michael Dzjaparidze
Michael Dzjaparidze le 13 Déc 2011
this doesn't return the correct result for
b = [1 2 1 0; 0 1.2 0 0; 0 0 0 3; 1.6 0 4 2.2; 1 1 1 1];
it returns
b =
1.0000 2.0000 -1.0000 0
0 1.2000 0 0
0 0 0 3.0000
-1.6000 0 4.0000 -2.2000
instead of
b =
1.0000 2.0000 -1.0000 0
0 -1.2000 0 0
0 0 0 3.0000
-1.6000 0 4.0000 -2.2000
Jan
Jan le 13 Déc 2011
@Michael: Yes, it does not create the correct results.
It is always a good idea to test the code before accepting an answer, even if the code looks nice and even if it comes from a high-skilled programmer like Andrei.

Connectez-vous pour commenter.

Plus de réponses (2)

Jan
Jan le 13 Déc 2011
This negates every second element per column:
b = [1 2 1 0; 0 1.2 0 0; 0 0 0 3; 1.6 0 4 2.2];
gt0 = (b > 0);
even = mod(cumsum(gt0, 1) - 1, 2);
idx = and(gt0, even);
b(idx) = -b(idx);
[EDITED]: After reading your comment, I understand, that you want to negate one element per column only:
b = [1 2 1 0; 0 1.2 0 0; 0 0 0 3; 1.6 0 4 2.2];
gt0 = (b > 0);
idx = and(gt0, cumsum(gt0, 1) == 2);
b(idx) = -b(idx);
Or with a cleaner loop:
[nx, ny] = size(b);
for iy = 1:ny
isPositive = false;
for ix = 1:nx
if b(ix, iy) > 0
if isPositive
b(ix, iy) = -b(ix, iy);
break;
else
isPositive = true;
end
end
end
end
  1 commentaire
Michael Dzjaparidze
Michael Dzjaparidze le 13 Déc 2011
I'll go with the cleaner version of my original loop then. Thanks for all your help with this guys, really appreciated and once again apologies for the confusion. So this really should be the accepted answer instead the one marked previously.

Connectez-vous pour commenter.


Daniel Shub
Daniel Shub le 13 Déc 2011
While there are probably more efficient, and some would argue elegant, solutions, the real goal should be to do what you think makes the most sense. It is generally a bad idea to spend time trying to speed up sections of code until you know it is a bottle neck.
I like Andrei's solution, but it might be more confusing 6 months later to figure out what it is doing. I think loops often allow for easier documentation. I think a loop like yours with lots of useful comments is a very elegant solution.
My answer:
for column = 1:size(b, 2)
row = find(b(:, column), 1, 'last');
b(row, column) = -b(row, column);
end
  4 commentaires
Jan
Jan le 13 Déc 2011
@Daniel: Aaaaarrrgh. "every second" and "not be anymore than 2 nonzero". I still do not get the question completely.
I'm taking a break now and have a cup of coffee. Please fix all problems here. Thanks!
Michael Dzjaparidze
Michael Dzjaparidze le 13 Déc 2011
Yes, I wasn't completely clear in my original question, my apologies, so once more: I have a matrix of arbitrary dimensions filled with positive numbers or zeros. Every column of the matrix contains EXACTLY 2 nonzero positive entrees (no more, no less), each at an arbitrary row index. Now what I want is to negate the second of these nonzero positive entrees in every column.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Programming dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by