필터 지우기
필터 지우기

Merge nx3 double matrices present within a 3x1 cell

조회 수: 1 (최근 30일)
Alberto Acri
Alberto Acri 2022년 12월 11일
편집: Stephen23 2022년 12월 11일
I have the following generic code that generates me a 3x1 "output" cell.
a=randi(100,157,2);
b=randi(200,189,2);
c=randi(300,183,2);
mat={a;b;c};
for i=1:size(mat,1)
if i==1
aaa(i)={[mat{i,1} repmat(100,size(mat{i,1},1),1)]};
elseif i==2
aaa(i)={[mat{i,1} repmat(150,size(mat{i,1},1),1)]};
elseif i==3
aaa(i)={[mat{i,1} repmat(200,size(mat{i,1},1),1)]};
end
end
output=aaa';
The internal of "output" is characterized by:
157x3 double
189x3 double
183x3 double
I would like to try to merge all the matrix (nx3), present inside "output", into one array (an example like this: link)
I used the following code but it gives an error due to the fact that the number of rows in each matrix is different.
matrix = [];
for nbr = 1:3
matrix = [matrix, output{nbr,1}];
end
  댓글 수: 1
Stephen23
Stephen23 2022년 12월 11일
편집: Stephen23 2022년 12월 11일
Simpler code and much more efficient use of MATLAB:
a=randi(100,157,2);
b=randi(200,189,2);
c=randi(300,183,2);
tmp = {a,b,c};
vec = [100,150,200];
for k = 1:numel(tmp)
tmp{k}(:,3) = vec(k);
end
out = vertcat(tmp{:}) % comma-separated list
out = 529×3
59 31 100 58 60 100 35 98 100 65 48 100 97 91 100 56 49 100 94 77 100 33 4 100 11 89 100 4 56 100

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

채택된 답변

C B
C B 2022년 12월 11일
a=randi(100,157,2);
b=randi(200,189,2);
c=randi(300,183,2);
mat={a;b;c};
for i=1:size(mat,1)
if i==1
aaa(i)={[mat{i,1} repmat(100,size(mat{i,1},1),1)]};
elseif i==2
aaa(i)={[mat{i,1} repmat(150,size(mat{i,1},1),1)]};
elseif i==3
aaa(i)={[mat{i,1} repmat(200,size(mat{i,1},1),1)]};
end
end
output=aaa';
matrix = [];
for nbr = 1:3
matrix = [matrix; output{nbr,1}];
end
matrix
matrix = 529×3
97 80 100 12 3 100 38 40 100 54 71 100 96 94 100 25 50 100 81 59 100 4 76 100 73 85 100 54 37 100

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by