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

3 ビュー (過去 30 日間)
sharmin sathi
sharmin sathi 2022 年 1 月 11 日
コメント済み: sharmin sathi 2022 年 1 月 11 日
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]}
  1 件のコメント
sharmin sathi
sharmin sathi 2022 年 1 月 11 日
Index position are fixed in your code. How can i dynamic?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeConvert Image Type についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by