How to find values which are in specific range for each rows in matrix...?

for example...
X = [1 2 3 4 5 6 7 8 9;
2 3 4 5 6 7 8 9 1;
3 4 5 6 7 8 9 1 2;
4 5 6 7 8 9 1 2 3;
5 6 7 8 9 1 2 3 4];
I want to find values which are bigger than 7 and smaller than 3 in each rows.
And also I have to save the data, index of values.
when I made a code like blew,
for k =1:m
[row(k) col(k)]=find((X(k,:) > 7) && (X(k,:) < 3));
end
there is an error "Operands to the and && operators must be convertible to logical scalar values."
please... help me

 Risposta accettata

The loop is completely pointless in this case. Plus, once you've fixed the error with &&, if there's more than one value that is in range you'll be trying to assign the vectors returned by find to the scalar values row(k) and col(k) which will error. The logical operator for arrays is & not && (see find array elements that meet a condition and logical operators short circuit)
[row, col] = find(x > 3 & X < 7)
edit: and I presume you meant bigger than 3 and smaller than 7, not bigger than 7 and smaller than 3 as there's no number that can match these two conditions.
edit2: and if you want an answer similar to that of KSSV:
[row, col] = find(X < 7 & X > 3); %or whatever the condition is
indx = accumarray(row, col, [], @(c) {c});
Again, no loop needed

Più risposte (0)

Categorie

Scopri di più su Mathematics in Centro assistenza e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by