How to determine sign change in a matrix for each coulmn separatly

Hello, I have three plots represented by bo matrix I want to calculate the points at which the sign changed for each curve separately. I wrote the following code and I got ZZ but ZZ is on row and I can't differentiate between sign change in each single curve. thanks in advance Hazem
clear all;close all;
b0=[1,2,3,-2,-3,7;3,4,7,-5,-3,4,;-3,-4,6,7,-1,-2];b0=b0';
ee=[1 2 3 4 5 6];plot(ee,b0)
ZZ=[];
for ib=1:size(b0,2)
for ie=1:size(b0,1)-1
if b0(ie,ib)>0 && b0(ie+1,ib)<=0 || b0(ie,ib)<0 && b0(ie+1,ib)>=0
%if b0(ie)>0 && b0(ie+1)<=0 || b0(ie)<0 && b0(ie+1)>=0
Z=ee(ie);
ZZ=[ZZ Z];l=min(ZZ);
end
end end

 Accepted Answer

I am not certain what you want, but this will give you the row indices in each column where the sign changes:
b0=[1,2,3,-2,-3,7;3,4,7,-5,-3,4,;-3,-4,6,7,-1,-2]';
ee=[1 2 3 4 5 6];plot(ee,b0')
sc_idx = (b0 .* circshift(b0, [1 0]) < 0); % Circularl Shift To Detect Sign Changes
[sc_posr, sc_posc] = find( sc_idx ); % Find Rows & Columns Of Sign Changes
ZZ = reshape(sc_posr, [], size(b0,2)); % Matrix Of Row Indices Of Sign Changes, By Column
ZZ =
4 4 3
6 6 5

6 Comments

This is exactly what I want thanks a lot :) but sometime there are column where there is no sign change and there will be an error in reshape. as followed: clear all close all; b0=[1,2,3,-2,-3,7;3,4,7,-5,-3,4;1,2,3,4,5,5;-3,-4,6,7,-1,-2]'; ee=[1 2 3 4 5 6];
sc_idx = (b0 .* circshift(b0, [1 0]) < 0); % Circularl Shift To Detect Sign Changes [sc_posr, sc_posc] = find( sc_idx ); % Find Rows & Columns Of Sign Changes ZZ = reshape(sc_posr, [], size(b0,2)); ??? Error using ==> reshape Product of known dimensions, 4, not divisible into total number of elements, 6.
This is as robust as I can make my routine:
sc_idx = (b0 .* circshift(b0, [1 0]) < 0); % Circular Shift To Detect Sign Changes
for k1 = 1:size(b0,2)
ZZ{k1} = find( sc_idx(:,k1) ); % Find Rows Of Sign Changes For Column k1
end
Ir creates a cell array to account for different numbers of sign changes in each column. (Nothing else in my code changes.)
I noted another problem, if we change the sign of the last element in the last coulmn we will have''in sc_idx'' four points >0 which is not true (there are only three changes of signs).
That’s the ‘end effect’. Logically, the first element in each row of ‘sc_idx’ can never be 1 because the first row in each column cannot indicate a sign change.
Change the loop to:
for k1 = 1:size(b0,2)
sc_idx(1,k1) = 0;
ZZ{k1} = find( sc_idx(:,k1) ); % Find Rows Of Sign Changes For Column k1
end
Thank you Star Stride. it's ok equating the first raw in sc_idx to zero.

Sign in to comment.

More Answers (1)

If you find the logical condition of the locations in b0 where the value is greater than or equal to zero, you can find the location of sign changes by taking the difference of each row with the previous row. A sign change would then have a value of +1 or -1 (or rather the non-zero values).
idxP = b0 >= 0; % Logical vector of non-negative values
diff(idxP) ~= 0
ans =
0 0 0 0 0 0
1 1 0 1 0 1
So where this condition is true we have a 1 (true) value. So for the first curve (column 1) we can see a sign change occurs from the 2nd to 3rd entry.

1 Comment

Hello Brendan, Thanks a lot for your help.I used your code but I added third raw to diff(idxP) ~= 0 because I need it to have the same dimensions like b0.

Sign in to comment.

Categories

Find more on App Building in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!