MATLAB Answers

save values in array

2 views (last 30 days)
Na
Na on 16 Mar 2020
Commented: Bhaskar R on 16 Mar 2020
I have
A={[1,6,3,2],[3,5,6]};
all_el =[];
for i=1:length(A)
all_el(end)=A{i}
end
I want to have this result
all_ell=[1,2,3,5,6]

  0 Comments

Sign in to comment.

Accepted Answer

Bhaskar R
Bhaskar R on 16 Mar 2020
all_el = unique([A{:}]);

  2 Comments

Na
Na on 16 Mar 2020
if A is
A={{[1,6,3,2]},{[3,5,6]}};
all_el = unique([A{:}]);
I have error
Bhaskar R
Bhaskar R on 16 Mar 2020
int_res = cellfun(@(x)[x{:}], A, 'UniformOutput', false);
all_el = unique([int_res{:}]);

Sign in to comment.

More Answers (1)

Sriram Tadavarty
Sriram Tadavarty on 16 Mar 2020
Edited: Sriram Tadavarty on 16 Mar 2020
Hi there,
It is not pretty clear as what you wanted to do.
To get the desired output, perform the following:
A={[1,6,3,2],[3,5,6]};
% With for loops
all_el =[];
for i=1:length(A)
all_el=[A{i} all_el];
end
all_el = unique(all_el);
% Without for loops
all_el = unique([all_el{:}])
Hope this helps.
Regards,
Sriram

  0 Comments

Sign in to comment.