Extract specific data from 3D matrix
47 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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.
Risposte (2)
Iuliu Ardelean
il 30 Gen 2021
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
il 31 Gen 2021
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 Commenti
Walter Roberson
il 31 Gen 2021
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)
Vedere anche
Categorie
Scopri di più su Logical in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!