How to determine sign change in a matrix for each coulmn separatly
5 views (last 30 days)
Show older comments
Hazem Abdelsalam
on 30 Jul 2015
Commented: Star Strider
on 31 Jul 2015
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
0 Comments
Accepted Answer
Star Strider
on 30 Jul 2015
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
More Answers (1)
Brendan Hamm
on 30 Jul 2015
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.
See Also
Categories
Find more on Historical Contests 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!