3D arrays manipulation and use of if else statments

2 vues (au cours des 30 derniers jours)
Mantas Vaitonis
Mantas Vaitonis le 21 Juin 2018
Rouvert : Mantas Vaitonis le 25 Juin 2018
Hello Everyone,
Here is the situation. There are three 3D arrays. S (1x5x1000) that stores elements from 1:5, P (1x5x1000) that stores pairs for S, and idx (1x5x1000) to invoke pair. For example S is always the same and (1x5x1)=1 2 3 4 5, p (1x5x1)=3 1 5 2 3 and idx (1x5x1)=1 0 1 1 1. We have the situation where are two same (5-3 and 3-5) pairs with invoke signal, we need to change invoke signal from 1 to 0 that would look like 1 0 0 1 1, and we need to do this for the whole 3D array. Is there a way to achieve this without for loop, because this part is time sensitive? Simplified code
if idx(i)==1
if p(i)=s(p(i))
idx(i)=0
end
end
  6 commentaires
Mantas Vaitonis
Mantas Vaitonis le 21 Juin 2018
Modifié(e) : dpb le 21 Juin 2018
Here is a small sample:
S(:,:,1) =1 2 3 4 5
P(:,:,1) =3 1 1 3 2
idx(:,:,1) =1 0 1 1 0
in this case S(1,1,1) with P(1,1,1) is same pair as S(1,3,1) with P(1,3,1), thus idx should change its invode index from 1 to 0
idx[0 0 1 1 0]
Ex 2
S(:,:,2) =1 2 3 4 5
P(:,:,2) =5 4 3 1 1
idx(:,:,2) =1 0 1 1 0
in this case S(1,3,2) with P(1,3,2) pair can not be pair with it self, thus idx should be change to
idx [1 0 0 1 0]
Ex 3
S(:,:,3) =1 2 3 4 5
P(:,:,3) =4 5 2 1 5
idx(:,:,3) =1 0 0 1 1
in this case S(1,1,3) with P(1,1,3) is same pair as S(1,4,1) with P(1,4,1) and S(1,5,3) with P(1,5,3) can not be paired to it self, thus idx should be
idx[0 0 0 1 0]
Maybe now it is more clear?
Mantas Vaitonis
Mantas Vaitonis le 22 Juin 2018
Modifié(e) : Mantas Vaitonis le 22 Juin 2018
If i convert 3D arrays to 2D I can achieve what I want, but it is very slow.
cc=P(P);
[nn1,nn2]=size(P);
for j=1:nn1
idx1(j)={find(idx(j,:)>0)};
tempIdx=ones(size(idx1{j}));
for i=1:nn2
if(i==cc(j,i))
if any(cc(j,i)==idx1{j})
if tempIdx(cc(j,i)==idx1{j})==1
idx1{j}(cc(j,i)==idx1{j})=NaN;
tempIdx(pairs2(j,i))=0;
end
end
end
end
end
There was a mistake in original code.

Connectez-vous pour commenter.

Réponses (0)

Catégories

En savoir plus sur Matrices and Arrays dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by