Filter löschen
Filter löschen

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

2 Ansichten (letzte 30 Tage)
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.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 11 Jan. 2022
Bearbeitet: Walter Roberson am 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 Kommentare
Walter Roberson
Walter Roberson am 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'}

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Simon Chan
Simon Chan am 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]}

Kategorien

Mehr zu Convert Image Type finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by