简体   繁体   中英

How to find the first zero column (or last non-zero column) in an array, greater than a certain column value

I have an array in which the first two columns are zero for all entries. The array has non-zero values for some number of columns and then zeros in the rest. I wish to find the column that has the final non-zero (or first zero) entry that is not column 1 or 2, for plotting. I have tried

find(b(i,:)==0,1)

which of course returns 1.

I have tried

find(b(i,:)~=0,1)

which I thought would work, but bizarrely returns '2'. I thought that find(b(i,:)~=0,1, 'last') might work instead, which I have seen being suggested as a MATLAB command on various Stack Overflow responses, however I still get '2'!

Any help would be much appreciated.

You need the second output argument of find which represents the column subscript. ie

[~, cfirst] = find(b,1);  %to find the column subscript of the first non-zero value
[~, clast] = find(b,1,'last');  %to find the column subscript of the last non-zero value

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM