How to create a matrice from selected rows from other matrices

조회 수: 1 (최근 30일)
Jonathan Demmer
Jonathan Demmer 2021년 3월 18일
댓글: Rik 2021년 3월 18일
Hello all,
I would like to select a specific row (4187) from n matrices (n =20) and add these rows in a new matrice. I wrote the code below, I thought that every file loaded will add the line selected in the new matrice. However after the first iteration the matrice holy_lat_final becomes a matrice (1,1436) instead of staying (20,1436), and so the second iteration cant be done... can someone help me pelase?
file_lat_1 = 'October_01_2014_surface_lat_##.mat';
file_lon_1 = 'October_01_2014_surface_lon_##.mat';
Holy_lat_final = zeros (20,1436);
Holy_lon_final = zeros (20,1436);
n = 20; % number particles release per site
for i = 1:n % particle release
file_lat_1(29:30)=sprintf('%02.0f',i);
file_lon_1(29:30)=sprintf('%02.0f',i);
Holy_lat = load(file_lat_1);
Holy_lon = load(file_lon_1);
Holy_lat_final = Holy_lat_final(i,:) + Holy_lat.lat(4187,1:1436);
Holy_lon_final = Holy_lon_final(i,:) + Holy_lon.lon(4187,1:1436);
end

채택된 답변

Rik
Rik 2021년 3월 18일
You overwrote the entire variable. See the edits I made to your code.
file_lat_1_fmt = 'October_01_2014_surface_lat_%02.0f.mat';
file_lon_1_fmt = 'October_01_2014_surface_lon_%02.0f.mat';
n = 20; % number particles release per site
Holy_lat_final = zeros (n,1436);
Holy_lon_final = zeros (n,1436);
for i = 1:n % particle release
file_lat_1(29:30)=sprintf(file_lat_1_fmt,i);
file_lon_1(29:30)=sprintf(file_lon_1_fmt,i);
Holy_lat = load(file_lat_1);
Holy_lon = load(file_lon_1);
Holy_lat_final(i,:) = Holy_lat.lat(4187,1:1436);
Holy_lon_final(i,:) = Holy_lon.lon(4187,1:1436);
end
  댓글 수: 2
Jonathan Demmer
Jonathan Demmer 2021년 3월 18일
Perfect!! thank you very much!!!
Rik
Rik 2021년 3월 18일
You're welcome. If my answer solved your issue, please consider marking it as accepted answer, if not, feel free to post a comment with your remaining issues.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Particle Swarm에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by