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

조회 수: 3 (최근 30일)
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.

채택된 답변

Walter Roberson
Walter Roberson 2022년 1월 11일
편집: Walter Roberson 2022년 1월 11일
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
Walter Roberson
Walter Roberson 2022년 1월 11일
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 2022년 1월 11일
Its work correctly.
Thank you so much.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Simon Chan
Simon Chan 2022년 1월 11일
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]}

카테고리

Help CenterFile Exchange에서 Convert Image Type에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by