how to find zero value with find

209 ビュー (過去 30 日間)
alize beemiel
alize beemiel 2022 年 2 月 1 日
コメント済み: alize beemiel 2022 年 2 月 1 日
BC =[0 0 0
0 1 0
0 0 0
0 0 1
1 1 1
0 0 1
0 0 0
0 1 0
0 0 0]
U=1-BC
f=find(U);
and f is
f =[1 2 3 4 6 7 8 9 10 12 13 15 16 18 19 20 21 25 26 27]
but I was confused because it's clear that the exact value is
and this is what I want to have
i_want=[1 2 3 4 6 7 8 9 10 11 16 17 19 20 21 22 24 25 26 27]
Can someone help me Where is my mistake?

回答 (3 件)

Image Analyst
Image Analyst 2022 年 2 月 1 日
Try this. It will give you two vectors, rows and columns, which are the row and column location that the corresponding zero can be found at in the original matrix.
BC =[0 0 0
0 1 0
0 0 0
0 0 1
1 1 1
0 0 1
0 0 0
0 1 0
0 0 0];
[rows, columns] = find(BC == 0)
rows = 20×1
1 2 3 4 6 7 8 9 1 3
columns = 20×1
1 1 1 1 1 1 1 1 2 2
  3 件のコメント
Image Analyst
Image Analyst 2022 年 2 月 1 日
That's not good. You're basically getting a linear index but not in the order that you could use it to do vectorized operations. You're taking the linear index going across rows, and then down, while the way MATLAB wants it is going down rows first and then across column-by-column. So for example if you wanted to set all those values to 99 you could not do
BC(i_want) = 99;
with the way you asked for. However if you did
linearIndexes = find(BC == 0);
then you could do that. But actually you don't even need linear indexes -- you can just use logical indexes like this:
logicalIndexes = BC == 0;
BC(logicalIndexes) = 99; % For example set all zeros to the value 99.
I strongly urge you to not do what you asked for, and what you think you want to do, and to just use logical or linear indexes in the order MATLAB needs them, and avoid transposes and other stuff to get something that won't be useful to you.
I mean, with your numbers, you can't even use ind2sub() to get the actual (row, column) indexes.
alize beemiel
alize beemiel 2022 年 2 月 1 日
thank you sir
very interesting all of this

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


Turlough Hughes
Turlough Hughes 2022 年 2 月 1 日
Try the following:
find(BC==0)
or
find(~BC)
  4 件のコメント
Turlough Hughes
Turlough Hughes 2022 年 2 月 1 日
編集済み: Turlough Hughes 2022 年 2 月 1 日
My bad, I skimmed over your question a little too quickly...
Your data:
BC =[0 0 0
0 1 0
0 0 0
0 0 1
1 1 1
0 0 1
0 0 0
0 1 0
0 0 0];
What you're looking for is an index which counts along the rows of BC, this is a linear index but not the type that MATLAB uses. The convention for linear indexing in MATLAB is called column-major order, and involves counting down through each column, and moving left to right as shown in this figure:
(the figure shows linear indexing on the left and subscript indexing on the right)
Yes, you can obtain the array that you've described:
rowOrderIndex = find(~BC.')
idx = 20×1
1 2 3 4 6 7 8 9 10 11
and it does equal to i_want
i_want=[1 2 3 4 6 7 8 9 10 11 16 17 19 20 21 22 24 25 26 27].';
isequal(rowOrderIndex,i_want)
ans = logical
1
But it's not an index for the zeros:
BC(i_want).'
ans = 1×20
0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 1 1 0 0 0
They're located at:
idx = find(BC==0);
BC(idx).'
ans = 1×20
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
alize beemiel
alize beemiel 2022 年 2 月 1 日
thank you too for your answering
all this informations are very helpfull
thanks very much

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


Les Beckham
Les Beckham 2022 年 2 月 1 日
編集済み: Les Beckham 2022 年 2 月 1 日
Matlab will index down the columns first. It appears that you want to go across the rows first.
So, just transpose the matrix.
BC =[0 0 0
0 1 0
0 0 0
0 0 1
1 1 1
0 0 1
0 0 0
0 1 0
0 0 0];
i_want = find(BC' == 0)'
i_want = 1×20
1 2 3 4 6 7 8 9 10 11 16 17 19 20 21 22 24 25 26 27
  2 件のコメント
Les Beckham
Les Beckham 2022 年 2 月 1 日
Image Analyst's comment above is 100% correct that, even though my answer gives you what you think you want, it probably isn't what you really need.
alize beemiel
alize beemiel 2022 年 2 月 1 日
oh
thank you very much
so easy ..
thanks very much
for all your helpfull

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

カテゴリ

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