Storing concatenated string array problem
1 view (last 30 days)
Show older comments
I have two 1xn matrices that I want to merge together. If array one held [1 PF Pop, 2 PF Pop, 3 PF Pop...] and the other contained [1 AF Pop, 2 AF Pop, 3 AF Pop], then I would like the result to be the concatenation and array of both elements.
% The desired result is shown below, a single matrix concatenated with both elements
|1 AF Pop 1 PF Pop, 1 AF Pop 2 PF Pop,...|
|2 AF Pop 1 PF Pop, 2 AF Pop 2 PF Pop,...|
|3 AF Pop 1 PF Pop, 3 AF Pop 2 PF Pop,...|
% my code so far is below
for P=1:3
F=P*3;
for G = 1:4
G=(G+1)*F;
divbytwo=G/2;
bact8(P,G)=divbytwo; %array of P+1 elements
end
end
bacttry=bact8;
bacttry(1,:)=[];
no_of_rows=size(bacttry);
no_of_columns=no_of_rows;
no_of_columns(:,1)=[];
no_of_rows(:,2)=[];
array_columns=strcat(strtrim(cellstr(num2str((1:no_of_columns)'))'),' PF Pop');
array_rows=strcat(strtrim(cellstr(num2str((1:no_of_rows)'))'),' AF Pop');
1 Comment
Guillaume
on 19 Mar 2018
I have no idea which arrays you want to concatenate and into what. However you can create your two array_* much more simply with:
array_columns = compose('%d PF Pop', 1:no_of_columns);
array_rows = compose('%d AF Pop', 1:no_of_rows);
And your way of getting the number of rows and columns is extremely convoluted.
[no_of_rows, no_of_columns] = size(bacttry); %assuming that bacttry is 2D
Answers (1)
Venkata Siva Krishna Madala
on 19 Mar 2018
Hello Matt,
You should consider using cell arrays for this purpose. To concatenate 2 1xn arrays to a single nxn matrix the code will be :-
x1= {'1 PF Pop', '2 PF Pop', '3 PF Pop'}
x2= {'1 AF Pop', '2 AF Pop', '3 AF Pop'}
x={}
for i=1:numel(x1)
for j=1:numel(x2)
x{i,j}=strcat(x1{i},x2{j});
end
end
Regards,
Krishna Madala
0 Comments
See Also
Categories
Find more on Data Type Identification in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!