Effacer les filtres
Effacer les filtres

Find array elements that meet a condition an put them in a secondary array

5 vues (au cours des 30 derniers jours)
H R
H R le 21 Déc 2015
Commenté : Star Strider le 21 Déc 2015
Hello all, I have a 3D matrix A. For example suppose that A is as follows:
A(:,:,1) =
0.1835 0.0811 0.4359
0.3685 0.9294 0.4468
0.6256 0.7757 0.3063
0.7802 0.4868 0.5085
A(:,:,2) =
0.5108 0.3786 0.9390
0.8176 0.8116 0.8759
0.7948 0.5328 0.5502
0.6443 0.3507 0.6225
I want to find the elements in A that are >0.5 and put them in an array B. Each row of array B should be [i,j,k,value]. In above example my array B would be like:
3 1 1 0.6256
4 1 1 0.7802
2 2 1 0.9294
3 2 1 0.7757
...
After this I want to print array B as it is in a text file ('Example.txt'). Thank you for your help.

Réponse acceptée

Star Strider
Star Strider le 21 Déc 2015
The find and ind2sub will do exactly what you want:
A(:,:,1) = [0.1835 0.0811 0.4359
0.3685 0.9294 0.4468
0.6256 0.7757 0.3063
0.7802 0.4868 0.5085];
A(:,:,2) = [0.5108 0.3786 0.9390
0.8176 0.8116 0.8759
0.7948 0.5328 0.5502
0.6443 0.3507 0.6225];
idx = find(A > 0.5);
[i,j,k] = ind2sub(size(A), idx);
B = [i j k A(idx)]
B =
3 1 1 0.6256
4 1 1 0.7802
2 2 1 0.9294
3 2 1 0.7757
4 3 1 0.5085
1 1 2 0.5108
2 1 2 0.8176
3 1 2 0.7948
4 1 2 0.6443
2 2 2 0.8116
3 2 2 0.5328
1 3 2 0.939
2 3 2 0.8759
3 3 2 0.5502
4 3 2 0.6225

Plus de réponses (4)

Image Analyst
Image Analyst le 21 Déc 2015
Here are two different ways, a for loop way, and a vectorized way:
A(:,:,1) =[...
0.1835 0.0811 0.4359
0.3685 0.9294 0.4468
0.6256 0.7757 0.3063
0.7802 0.4868 0.5085];
A(:,:,2) =[...
0.5108 0.3786 0.9390
0.8176 0.8116 0.8759
0.7948 0.5328 0.5502
0.6443 0.3507 0.6225]
% In tuitive brute force "for" loop way:
numRows = nnz(A(:)>0.5);
out = zeros(numRows, 4);
currentRow = 1
for z = 1 : size(A, 3)
for row = 1 : size(A, 1)
for col = 1 : size(A, 2)
if A(row, col, z) > 0.5
out(currentRow, 1:4) = [row, col, z, A(row, col, z)];
currentRow = currentRow + 1;
end
end
end
end
out
% Vectorized way:
bigA = A(A(:)>0.5);
actualSubscripts = find(A(:) > 0.5)
[row, col, z] = ind2sub(size(A), actualSubscripts)
out = [row, col, z, bigA]

Azzi Abdelmalek
Azzi Abdelmalek le 21 Déc 2015
ll=find(A>0.5)
[ii,jj,kk]=ind2sub(size(A),ll)
out=[ii,jj,kk,A(ll)]

Andrei Bobrov
Andrei Bobrov le 21 Déc 2015
t = A > .5;
[m,n,~] = size(A);
[ii,jj] = find(t);
B = [ii,rem(jj-1,n)+1,ceil(jj/n),A(t)];

H R
H R le 21 Déc 2015
Modifié(e) : H R le 21 Déc 2015
Thank you for your help.
As a secondary filtering on B, I would like to only retain the rows when the first element of any row in B >2, and the second element of B in any row <3 and put them in a new array C. In other words, my array C has the following properties [i>2,j<3,k, value]
  1 commentaire
Star Strider
Star Strider le 21 Déc 2015
This seems to work (continuing my previous code):
Crows = (B(:,1)>2) & (B(:,2)<3); % Logical Vector: 1=Meets Criteria
C = [B(Crows,1) B(Crows,2) k(Crows) B(Crows,4)] % New Filtered Matrix
C =
3 1 1 0.6256
4 1 1 0.7802
3 2 1 0.7757
3 1 2 0.7948
4 1 2 0.6443
3 2 2 0.5328

Connectez-vous pour commenter.

Catégories

En savoir plus sur Entering Commands dans Help Center et File Exchange

Tags

Aucun tag saisi pour le moment.

Community Treasure Hunt

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

Start Hunting!

Translated by