Extract specific data from 3D matrix
조회 수: 28 (최근 30일)
이전 댓글 표시
I would like to kindly ask you for the help with the issue of extracting specific values from 3D matrix. I have the variable A(10,10,3) containing the elements 1, 2, 3, and the second variable B(10,10,3) containing pixel values. I need to extract and store the elements from each layer of B which are in the same position as elements 1 from A. Thus, these values should be stored in a cell array (it should contain 3 cells). I was able to perform this task for one 2D matrix, but in the case of 3D matrix I am not able to do that. I would like to kindly ask you for help.
답변 (2개)
Iuliu Ardelean
2021년 1월 30일
size = 10;
A = randi(3, [size size 3]); % matrix containing elements 1, 2 and 3
B = randi(255, [size size 3]); % RGB matrix
[row, col] = find(A==1);
% I wasn't sure if you wanted to extract only one of R, G, or B,
% OR you wanted to extract all three R, G, and B at a same time...
% so option 1: if you want to extract only one of R, G, and B at a time:
pixel_values = zeros(size, size, 3);
pixel_values(A==1) = B(A==1)
% and option 2: if you want to extract all three R, G, and B at the same time:
pixel_values = zeros(size, size, 3);
col = mod(col,size); % this is because find returns linear indices if matrix is multidimensional
col(col == 0) = size;
pixel_values(row, col, :) = B(row, col, :)
Walter Roberson
2021년 1월 31일
A = randi(4, 5,5,3)
B = randi(9, 5,5,3)
idx = find(A==1);
temp = B(idx);
[R,C,P] = ind2sub(size(A),idx);
pix = arrayfun(@(p) temp(P==p).', 1:max(P), 'uniform', 0);
celldisp(pix)
댓글 수: 2
Walter Roberson
2021년 1월 31일
A = randi(4, 5,5,3)
B = randi(9, 5,5,3)
SELECT = @(M,W) M(W);
pix = arrayfun(@(p) SELECT(B(:,:,p), A(:,:,p)==1).', 1:size(A,3), 'uniform', 0);
celldisp(pix)
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!