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

11 ビュー (過去 30 日間)
Hazem Abdelsalam
Hazem Abdelsalam 2015 年 7 月 30 日
コメント済み: Star Strider 2015 年 7 月 31 日
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.
Star Strider
Star Strider 2015 年 7 月 31 日
My pleasure.

サインインしてコメントする。

その他の回答 (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 Center および File ExchangeHistorical Contests についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by