Combine columns of multiple arrays using for loop
Afficher commentaires plus anciens
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 commentaires
Paolo
le 21 Mai 2018
You mean for all 967 columns?
Charlotte Findlay
le 21 Mai 2018
James Tursa
le 21 Mai 2018
Modifié(e) : James Tursa
le 21 Mai 2018
What would the dimensions of the result 'test' be? Are you wanting to stack these variables vertically or horizontally?
Charlotte Findlay
le 21 Mai 2018
Réponse acceptée
Plus de réponses (1)
Paolo
le 21 Mai 2018
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.
6 commentaires
I'm not sure why this answer was deleted to then be reposted more or less the same. I'll repost my comment:
If that is indeed the output wished, then using a loop for that is a complete waste of time:
output = permute(cat(3, lat, lon, rng, z), [1 3 2]);
is all that is required. The above concatenate the 4 arrays in the 3rd dimension, then swap 2nd and 3rd dimension
Paolo
le 21 Mai 2018
Apologies, when I updated the rows and columns with the new values and tested for equality using isequal I was getting a 0 as output, so I thought something was wrong, whereas its just a matter of using isequaln.
Absolutely, its only one of the possible solutions, however Charlotte asked for a solution which uses a for loop in her first post.
Charlotte Findlay
le 21 Mai 2018
Paolo
le 21 Mai 2018
Simply increase the second dimension to 5 and store the index in the last column.
output = zeros(rows,5,columns);
for i = 1:columns
output(:,1:4,i) = [lat(:,i) lon(:,i) rng(:,i) z(:,i)];
output(:,5,i) = i;
end
Charlotte Findlay
le 21 Mai 2018
Paolo
le 21 Mai 2018
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.
Catégories
En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!