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

조회 수: 11 (최근 30일)
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

채택된 답변

Star Strider
Star Strider 2015년 7월 30일
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
Hazem Abdelsalam
Hazem Abdelsalam 2015년 7월 31일
편집: Stephen23 2015년 7월 31일
Thank you Star Stride. it's ok equating the first raw in sc_idx to zero.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Brendan Hamm
Brendan Hamm 2015년 7월 30일
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
Hazem Abdelsalam
Hazem Abdelsalam 2015년 7월 31일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Historical Contests에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by