Combine columns of multiple arrays using for loop

조회 수: 3 (최근 30일)
Charlotte Findlay
Charlotte Findlay 2018년 5월 21일
댓글: Paolo 2018년 5월 21일
I am trying to use a for loop to combine columns of multiple arrays in MATLAB.
I want to take column 1 (or i) of the first variable (lat) and combine this with column 1 (or i) of the other three variables (lon, rng, and z), similar to the output expected from the code below.
if true
% Example of output wanted:
test = [lat(:,1) lon(:,1) rng(:,1) z(:,1)];
end
However, I want to repeat this for all 360 columns in each of the four arrays (see attached matlab file).
How would I do this with a for loop?
  댓글 수: 4
James Tursa
James Tursa 2018년 5월 21일
편집: James Tursa 2018년 5월 21일
What would the dimensions of the result 'test' be? Are you wanting to stack these variables vertically or horizontally?
Charlotte Findlay
Charlotte Findlay 2018년 5월 21일
Preferably the output would be vertical with the ability to decipher where one column has ended and another starts (i.e. have another column which labels the 967 rows as 1 and then the next 967 rows as 2, etc.)

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

채택된 답변

Guillaume
Guillaume 2018년 5월 21일
편집: Guillaume 2018년 5월 21일
Preferably the output would be vertical with the ability to decipher where one column has ended and another starts
Is this for storage or display?.
If for storage, then simply concatenate the variables.
allvars = cat(3, lat, lon, rng, z); %concatenate the variables in the 3rd dimension
%allvars(:, n, :) correspond to column n of the variables
If you wish you can then permute dimensions so that your original variables are then columns,
allvars = permute(cat(3, lat, lon, rng, z), [1 3 2]);
%allvars(:, :, n) corresponds to column n of the variables
edit:
i.e. have another column which labels the first 967 rows as 1 and then the next 967 rows as 2, etc
That can be done as well:
allvars = [reshape(cat(3, lat, lon, rng, z), [], 4), repelem(1:size(lat, 2), size(lat, 1))']
The last column is the index of the original column
  댓글 수: 1
Charlotte Findlay
Charlotte Findlay 2018년 5월 21일
편집: Charlotte Findlay 2018년 5월 21일
Hi Guillaume,
I have edited your above code as follows and it seems to be giving me the output I require in columns:
if true
allvars = permute(cat(3, lat, lon, rng, z), [1 3 2]);
allvars(:, :, 1:360)
end
Would it be possible to add another column to this which denotes 'transect' one i.e. the first 967 rows and 4 columns, and then transect two, etc?
Edit
That additional comment seemed to do just the trick! Thank you so much for your help Guillaume!

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

추가 답변 (1개)

Paolo
Paolo 2018년 5월 21일
Hi Charlotte,
The following code creates a multidimensional matrix with 967 rows (first dimension), 4 columns (second dimension) and 360 elements (third dimension).
load('ArrayValuesForLoop_20180521_CFindlay.mat');
%Dimensions.
[rows,columns] = size(lat);
%Number of sheets to combine.
n = 4;
%Initialisation.
output = zeros(rows,n,columns);
for i = 1:columns
output(:,:,i) = [lat(:,i) lon(:,i) rng(:,i) z(:,i)];
end
You can access the output with:
output(:,:,n)
where n is the element you are interested in. As in your example, test will be equal to output(:,:,1).
test = [lat(:,1) lon(:,1) rng(:,1) z(:,1)];
is equivalent to output(:,:,1).
test = [lat(:,2) lon(:,2) rng(:,2) z(:,2)];
is equivalent to output(:,:,2), and so on.
You must use isequaln to treat the NaN value as identical.
  댓글 수: 6
Charlotte Findlay
Charlotte Findlay 2018년 5월 21일
Hi Paolo,
Really appreciate your help with this query, and your solution has been made note of in my code! Definitely always multiple solutions to one problem! :)
Paolo
Paolo 2018년 5월 21일
You are welcome, there are many ways this can be done. Guillaume's solution is indeed more elegant, I used a for loop in my solution since that was what you asked for.

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by