What would be the MatLab code to get the output from the input image?.
1 次查看(过去 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.
0 个评论
采纳的回答
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
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
colidx = sum(cumprod(cellfun(@isempty, INPUT), 2),2) + 1;
OUTPUT = INPUT(sub2ind(size(INPUT), (1:size(INPUT,1)).', colidx));
OUTPUT
更多回答(1 个)
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 Center 和 File Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!