How to create a struct in Matlab with parts of anothers structs? [closed]
up vote
0
down vote
favorite
I have several struct of different dimensions in Matlab. Let us suppose that they were 5, the first is 1x100
, the second 1x250
, the third 1x200
, the fourth 1x100
, and the fifth 1x150
. I want to assemble a new structure containing only the last element of the each previous structures, that is, I want to get a struct of the 1x5
form. How can I do this?
matlab struct structure
closed as too broad by Wolfie, Sardar Usama, Cris Luengo, gnat, il_raffa Nov 13 at 8:30
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
up vote
0
down vote
favorite
I have several struct of different dimensions in Matlab. Let us suppose that they were 5, the first is 1x100
, the second 1x250
, the third 1x200
, the fourth 1x100
, and the fifth 1x150
. I want to assemble a new structure containing only the last element of the each previous structures, that is, I want to get a struct of the 1x5
form. How can I do this?
matlab struct structure
closed as too broad by Wolfie, Sardar Usama, Cris Luengo, gnat, il_raffa Nov 13 at 8:30
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
1
Usingend
and a simple loop doesn't do the trick? Please provide a Minimal, Complete, and Verifiable example.
– Wolfie
Nov 10 at 14:58
1
Do all those structs share the same format (i.e. same fields)? Don't you actually have arrays of structs which you want to concatenate?
– Mefitico
Nov 10 at 20:48
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have several struct of different dimensions in Matlab. Let us suppose that they were 5, the first is 1x100
, the second 1x250
, the third 1x200
, the fourth 1x100
, and the fifth 1x150
. I want to assemble a new structure containing only the last element of the each previous structures, that is, I want to get a struct of the 1x5
form. How can I do this?
matlab struct structure
I have several struct of different dimensions in Matlab. Let us suppose that they were 5, the first is 1x100
, the second 1x250
, the third 1x200
, the fourth 1x100
, and the fifth 1x150
. I want to assemble a new structure containing only the last element of the each previous structures, that is, I want to get a struct of the 1x5
form. How can I do this?
matlab struct structure
matlab struct structure
asked Nov 10 at 14:09
Elcio Silveira
155
155
closed as too broad by Wolfie, Sardar Usama, Cris Luengo, gnat, il_raffa Nov 13 at 8:30
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
closed as too broad by Wolfie, Sardar Usama, Cris Luengo, gnat, il_raffa Nov 13 at 8:30
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
1
Usingend
and a simple loop doesn't do the trick? Please provide a Minimal, Complete, and Verifiable example.
– Wolfie
Nov 10 at 14:58
1
Do all those structs share the same format (i.e. same fields)? Don't you actually have arrays of structs which you want to concatenate?
– Mefitico
Nov 10 at 20:48
add a comment |
1
Usingend
and a simple loop doesn't do the trick? Please provide a Minimal, Complete, and Verifiable example.
– Wolfie
Nov 10 at 14:58
1
Do all those structs share the same format (i.e. same fields)? Don't you actually have arrays of structs which you want to concatenate?
– Mefitico
Nov 10 at 20:48
1
1
Using
end
and a simple loop doesn't do the trick? Please provide a Minimal, Complete, and Verifiable example.– Wolfie
Nov 10 at 14:58
Using
end
and a simple loop doesn't do the trick? Please provide a Minimal, Complete, and Verifiable example.– Wolfie
Nov 10 at 14:58
1
1
Do all those structs share the same format (i.e. same fields)? Don't you actually have arrays of structs which you want to concatenate?
– Mefitico
Nov 10 at 20:48
Do all those structs share the same format (i.e. same fields)? Don't you actually have arrays of structs which you want to concatenate?
– Mefitico
Nov 10 at 20:48
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
Create 5 row vectors, then make a row vector from the last elements like this:
>> a=[1:100];
>> b=[1:250];
>> c=[1:200];
>> d=[1:100];
>> e=[1:150];
>> the_lasts = [a(end), b(end), c(end), d(end), e(end)]
sthe_last =
100 250 200 100 150
>>
That also can be generalized into a function, using a file named:
ends_of.m :
function lasts = ends_of (varargin)
% prepare a result row vector with nargin dimention
lasts = zeros(1,nargin);
for i = 1:nargin
element = varargin(i); % get the arg
last = element(end); % find the last element
lasts(i) = last; % save it as index i
endfor
endfunction
Then it can be called like this :
>> ends_of ([1,2,3],[4,5,6],[1:44], [3:33], [1,2,3,2,1])
ans =
3 6 44 33 1
>>
@cris, thanks. i update the answer
– chenchuk
Nov 10 at 22:10
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
Create 5 row vectors, then make a row vector from the last elements like this:
>> a=[1:100];
>> b=[1:250];
>> c=[1:200];
>> d=[1:100];
>> e=[1:150];
>> the_lasts = [a(end), b(end), c(end), d(end), e(end)]
sthe_last =
100 250 200 100 150
>>
That also can be generalized into a function, using a file named:
ends_of.m :
function lasts = ends_of (varargin)
% prepare a result row vector with nargin dimention
lasts = zeros(1,nargin);
for i = 1:nargin
element = varargin(i); % get the arg
last = element(end); % find the last element
lasts(i) = last; % save it as index i
endfor
endfunction
Then it can be called like this :
>> ends_of ([1,2,3],[4,5,6],[1:44], [3:33], [1,2,3,2,1])
ans =
3 6 44 33 1
>>
@cris, thanks. i update the answer
– chenchuk
Nov 10 at 22:10
add a comment |
up vote
1
down vote
accepted
Create 5 row vectors, then make a row vector from the last elements like this:
>> a=[1:100];
>> b=[1:250];
>> c=[1:200];
>> d=[1:100];
>> e=[1:150];
>> the_lasts = [a(end), b(end), c(end), d(end), e(end)]
sthe_last =
100 250 200 100 150
>>
That also can be generalized into a function, using a file named:
ends_of.m :
function lasts = ends_of (varargin)
% prepare a result row vector with nargin dimention
lasts = zeros(1,nargin);
for i = 1:nargin
element = varargin(i); % get the arg
last = element(end); % find the last element
lasts(i) = last; % save it as index i
endfor
endfunction
Then it can be called like this :
>> ends_of ([1,2,3],[4,5,6],[1:44], [3:33], [1,2,3,2,1])
ans =
3 6 44 33 1
>>
@cris, thanks. i update the answer
– chenchuk
Nov 10 at 22:10
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Create 5 row vectors, then make a row vector from the last elements like this:
>> a=[1:100];
>> b=[1:250];
>> c=[1:200];
>> d=[1:100];
>> e=[1:150];
>> the_lasts = [a(end), b(end), c(end), d(end), e(end)]
sthe_last =
100 250 200 100 150
>>
That also can be generalized into a function, using a file named:
ends_of.m :
function lasts = ends_of (varargin)
% prepare a result row vector with nargin dimention
lasts = zeros(1,nargin);
for i = 1:nargin
element = varargin(i); % get the arg
last = element(end); % find the last element
lasts(i) = last; % save it as index i
endfor
endfunction
Then it can be called like this :
>> ends_of ([1,2,3],[4,5,6],[1:44], [3:33], [1,2,3,2,1])
ans =
3 6 44 33 1
>>
Create 5 row vectors, then make a row vector from the last elements like this:
>> a=[1:100];
>> b=[1:250];
>> c=[1:200];
>> d=[1:100];
>> e=[1:150];
>> the_lasts = [a(end), b(end), c(end), d(end), e(end)]
sthe_last =
100 250 200 100 150
>>
That also can be generalized into a function, using a file named:
ends_of.m :
function lasts = ends_of (varargin)
% prepare a result row vector with nargin dimention
lasts = zeros(1,nargin);
for i = 1:nargin
element = varargin(i); % get the arg
last = element(end); % find the last element
lasts(i) = last; % save it as index i
endfor
endfunction
Then it can be called like this :
>> ends_of ([1,2,3],[4,5,6],[1:44], [3:33], [1,2,3,2,1])
ans =
3 6 44 33 1
>>
edited Nov 11 at 10:06
answered Nov 10 at 20:18
chenchuk
1,94731322
1,94731322
@cris, thanks. i update the answer
– chenchuk
Nov 10 at 22:10
add a comment |
@cris, thanks. i update the answer
– chenchuk
Nov 10 at 22:10
@cris, thanks. i update the answer
– chenchuk
Nov 10 at 22:10
@cris, thanks. i update the answer
– chenchuk
Nov 10 at 22:10
add a comment |
1
Using
end
and a simple loop doesn't do the trick? Please provide a Minimal, Complete, and Verifiable example.– Wolfie
Nov 10 at 14:58
1
Do all those structs share the same format (i.e. same fields)? Don't you actually have arrays of structs which you want to concatenate?
– Mefitico
Nov 10 at 20:48