Effacer les filtres
Effacer les filtres

What would be the MatLab code to get the output from the input image?.

2 vues (au cours des 30 derniers jours)
I have four columns with four different input values in different positions. I want to combine 4 cells into one cell like the attached image.
pls help me.

Réponse acceptée

Walter Roberson
Walter Roberson le 11 Jan 2022
Modifié(e) : Walter Roberson le 11 Jan 2022
colidx = sum(cumprod(~cellfun(@isempty, INPUT), 2),2) + 1;
OUTPUT = INPUT(sub2ind(size(INPUT), 1:size(INPUT,1), colidx));
This code does not rely upon the columns having consistent values. It does, however, rely upon there being exactly one non-empty column per row.
  3 commentaires
Walter Roberson
Walter Roberson le 11 Jan 2022
names = {'A', 'B', 'C', 'D'};
index = [2 2 2 1 2 3 4 2 1 2];
nrow = length(index);
ncol = max(index);
INPUT = cell(nrow, ncol);
INPUT(:) = {''};
INPUT(sub2ind(size(INPUT), 1:nrow, index)) = names(index);
INPUT
INPUT = 10×4 cell array
{0×0 char} {'B' } {0×0 char} {0×0 char} {0×0 char} {'B' } {0×0 char} {0×0 char} {0×0 char} {'B' } {0×0 char} {0×0 char} {'A' } {0×0 char} {0×0 char} {0×0 char} {0×0 char} {'B' } {0×0 char} {0×0 char} {0×0 char} {0×0 char} {'C' } {0×0 char} {0×0 char} {0×0 char} {0×0 char} {'D' } {0×0 char} {'B' } {0×0 char} {0×0 char} {'A' } {0×0 char} {0×0 char} {0×0 char} {0×0 char} {'B' } {0×0 char} {0×0 char}
colidx = sum(cumprod(cellfun(@isempty, INPUT), 2),2) + 1;
OUTPUT = INPUT(sub2ind(size(INPUT), (1:size(INPUT,1)).', colidx));
OUTPUT
OUTPUT = 10×1 cell array
{'B'} {'B'} {'B'} {'A'} {'B'} {'C'} {'D'} {'B'} {'A'} {'B'}
sharmin sathi
sharmin sathi le 11 Jan 2022
Its work correctly.
Thank you so much.

Connectez-vous pour commenter.

Plus de réponses (1)

Simon Chan
Simon Chan le 11 Jan 2022
Try the following:
rawdata = num2cell((randi(10,10,4)));
index = [2 2 2 1 2 3 4 2 1 2]';
singlecell = cellfun(@(x,y) x(y), num2cell(rawdata,2),num2cell(index));
rawdata:
rawdata =
10×4 cell array
{[5]} {[ 6]} {[8]} {[ 6]}
{[1]} {[ 7]} {[5]} {[10]}
{[6]} {[ 5]} {[1]} {[ 7]}
{[5]} {[ 9]} {[3]} {[10]}
{[7]} {[ 8]} {[2]} {[ 3]}
{[7]} {[10]} {[3]} {[ 7]}
{[7]} {[ 6]} {[5]} {[ 3]}
{[1]} {[ 4]} {[6]} {[ 7]}
{[1]} {[ 2]} {[5]} {[ 7]}
{[4]} {[ 7]} {[9]} {[ 1]}
singlecell:
singlecell =
10×1 cell array
{[6]}
{[7]}
{[5]}
{[5]}
{[8]}
{[3]}
{[3]}
{[4]}
{[1]}
{[7]}

Catégories

En savoir plus sur Convert Image Type dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by