Extract specific data from 3D matrix
44 ビュー (過去 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!