3D arrays manipulation and use of if else statments

1 次查看(过去 30 天)
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 个评论
Mantas Vaitonis
Mantas Vaitonis 2018-6-21
编辑:dpb 2018-6-21
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 2018-6-22
编辑:Mantas Vaitonis 2018-6-22
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.

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 GPU Computing in MATLAB 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by