필터 지우기
필터 지우기

how to concatenate structure fields

조회 수: 5 (최근 30일)
Newman
Newman 2016년 7월 15일
댓글: Azzi Abdelmalek 2016년 7월 15일
Hello I have a structure s(i).f(j).d where i=1:40 and j=1:10.I want to concantenate all the fields in to one single matrix. for eg
for j=1:10
m(:,j)=s(1).f(j).d
with the following code above
this concatenates 10 column matrix into m .But this is just for s(1) how to proceed for s1..s2...s40 for all the 40 fields?

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2016년 7월 15일
Look at this example
s(1).f(1).d=11
s(1).f(2).d=12
s(2).f(1).d=21
s(2).f(2).d=22
a=s.f
n=numel(a)
for k=1:numel(s)
M(k,:)=[s(k).f(1:n).d]
end
  댓글 수: 4
Newman
Newman 2016년 7월 15일
this is my structure file
Azzi Abdelmalek
Azzi Abdelmalek 2016년 7월 15일
load lbpstructure
a=s.f;
n=numel(a);
M=[]
for k=1:numel(s)
b=[s(k).f(1:n).d];
M=[M;b];
end
M

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

추가 답변 (1개)

Guillaume
Guillaume 2016년 7월 15일
Use a double for loop, then:
m = zeros(numel(s(1).f(1).d), numel(s(1).f(1)), numel(s)); %preallocation is advised
for sidx = 1:numel(s)
for fidx = 1:numel(s(sidx).f)
m(:, fidx, sidx) = s(sidx).f(fidx).d;
end
end
Note that I've concatenated the s1, ... s40 along the 3rd dimension. You didn't specify what you wanted exactly.
  댓글 수: 2
Newman
Newman 2016년 7월 15일
편집: Newman 2016년 7월 15일
This is exactly what I want: please see the image below
your code is returning m as 10304x10x40 double where as I want it as 10304x400.(400= 10 fields in s(1) or s(2) etc. so for 40 s(i) = 40x10=400)
Guillaume
Guillaume 2016년 7월 15일
Whichever shape you want for the output, the idea is still the same. Iterate over both arrays. To get the above:
m = zeros(numel(s(1).f(1).d, numel(s) * numel(s(1).f));
col = 1;
for sidx = 1 : numel(s)
for fidx = 1 : numel(f)
m(:, col) = s(sidx).f(idx).d;
col = col + 1;
end
end

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

카테고리

Help CenterFile Exchange에서 Wireless Communications에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by