Indexing a 3D array with a vector

16 vues (au cours des 30 derniers jours)
John
John le 12 Nov 2014
Commenté : John le 13 Nov 2014
% My question is best explained with an example;
% I would like to change each of the elements in column three of each page of exampleData to -100,
% if they are greater than 5. I have done this with a for loop, but would like to know if there is
% a more elegant way, using assignments.
% Creating example data;
x = [[1,2,3,4];[5,6,7,8];[2,8,9,7]];
y = [[1,3,5,3];[7,9,0,2];[2,4,5,4]];
z = [[2,4,6,6];[3,5,7,1];[8,9,3,3]];
exampleData = cat(3,x,y,z);
% For loop method - this gets the desired result, but I am interested to see if it can be done
% via an assignement;
b = exampleData; % using "b" just to make it more readable.
for pageInd = 1:length(exampleData(1,1,:));
cInd = b(:,3,pageInd)>5;
b(cInd,3,pageInd) = -100;
end
% Assignment method - this does not produce the desired result.
c = exampleData; % using "c" just to make it more readable.
cInd = c(:,3,:)>5;
c(cInd) = -100;
% Thanks very much for any help you give.
% John
>> b (desired result)
b(:,:,1) =
1 2 3 4
5 6 -100 8
2 8 -100 7
b(:,:,2) =
1 3 5 3
7 9 0 2
2 4 5 4
b(:,:,3) =
2 4 -100 6
3 5 -100 1
8 9 3 3
>> c (undesired result)
c(:,:,1) =
1 2 -100 4
-100 6 -100 8
-100 8 9 7
c(:,:,2) =
1 3 5 3
7 9 0 2
2 4 5 4
c(:,:,3) =
2 4 6 6
3 5 7 1
8 9 3 3

Réponse acceptée

David Young
David Young le 12 Nov 2014
c = b(:,3,:);
c(c > 5) = -100;
b(:,3,:) = c;
  1 commentaire
John
John le 13 Nov 2014
David,
Great answer thanks for that!
I’ve been looking through it trying to follow it.
I think at one stage we have the same index, mine was “b2(:,3,:)>5” and yours was “c = b(:,3,:)” followed by “(c > 5) = -100”. They then both obtain the same index values;
>> ind
ind(:,:,1) =
0
1
1
ind(:,:,2) =
0
0
0
ind(:,:,3) =
1
1
0
With your answer you then used this index on the third column only “c” and then applied that to exampleData. Whereas I tried to apply the index directly into exampleData.
I really like your solution, thanks very much for your help.

Connectez-vous pour commenter.

Plus de réponses (1)

pietro
pietro le 12 Nov 2014
try this:
x = [[1,2,3,4];[5,6,7,8];[2,8,9,7]];
y = [[1,3,5,3];[7,9,0,2];[2,4,5,4]];
z = [[2,4,6,6];[3,5,7,1];[8,9,3,3]];
exampleData = cat(3,x,y,z);
[I,J,K] = ind2sub(size(exampleData),find(exampleData>5));
b=exampleData;
for i=1:length(I)
b(I(i),J(i),K(i))=-100;
end
  1 commentaire
John
John le 13 Nov 2014
Hi pietro.
I tried your answer, but didn’t get the desired result. Also I was trying to avoid a for loop simply as I felt it should be possible by assignment.
Please note I’ve edited the original question as I got the example answers round the wrong way. Thanks very much for your help.
Your proposal gave; >> b
b(:,:,1) =
1 2 3 4
5 -100 -100 -100
2 -100 -100 -100
b(:,:,2) =
1 3 5 3
-100 -100 0 2
2 4 5 4
b(:,:,3) =
2 4 -100 -100
3 5 -100 1
-100 -100 3 3

Connectez-vous pour commenter.

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by