Storing concatenated string array problem

조회 수: 7 (최근 30일)
Matt Guenther
Matt Guenther 2018년 3월 15일
댓글: Guillaume 2018년 3월 19일
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
Guillaume
Guillaume 2018년 3월 19일
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

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

답변 (1개)

Venkata Siva Krishna Madala
Venkata Siva Krishna Madala 2018년 3월 19일
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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by