Indexing a 3D array with a vector

84 ビュー (過去 30 日間)
John
John 2014 年 11 月 12 日
コメント済み: John 2014 年 11 月 13 日
% 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

採用された回答

David Young
David Young 2014 年 11 月 12 日
c = b(:,3,:);
c(c > 5) = -100;
b(:,3,:) = c;
  1 件のコメント
John
John 2014 年 11 月 13 日
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.

サインインしてコメントする。

その他の回答 (1 件)

pietro
pietro 2014 年 11 月 12 日
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 件のコメント
John
John 2014 年 11 月 13 日
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

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by