How to insert values in a zeros matrix?

조회 수: 39 (최근 30일)
Aarti Soni
Aarti Soni 2022년 4월 16일
댓글: Voss 2022년 4월 18일
Hello everyone,
I have created a zeros metrix X = 720x1440, the interval of each grid is 0.25. I have another matrix Y= 129x135, this matrix is having values. Now I want to insert the values of Y matrix into the X matrix.
The location of Y matrix is, 6.5:0.25:38.5 (129) and 66.5:0.25:100.0 (135)
Thank you
  댓글 수: 3
the cyclist
the cyclist 2022년 4월 16일
You told us the grid interval, but we also need to know the grid starting point. Does X(1,1) correspond to grid location (0,0)?
Aarti Soni
Aarti Soni 2022년 4월 16일
0.25:0.25:360;
-89.75:0.25:90;
1440x720 is a global grid and 129x135 is the location of India. The location of India is given in question.
I hope this is clear.

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

답변 (1개)

Voss
Voss 2022년 4월 16일
all_lat = -89.75:0.25:90;
% according to the longitudes specified for India (66.5 to 100),
% longitude goes from -180 to 180, not 0 to 360:
all_lon = (0.25:0.25:360)-180;
india_lat = 6.5:0.25:38.5;
india_lon = 66.5:0.25:100.0;
whos *_lat *_lon
Name Size Bytes Class Attributes all_lat 1x720 5760 double all_lon 1x1440 11520 double india_lat 1x129 1032 double india_lon 1x135 1080 double
X = zeros(numel(all_lat),numel(all_lon)); % your zeros matrix
Y = rand(numel(india_lat),numel(india_lon)); % random matrix substituting for your Y matrix
% determine where Y belongs in X:
[~,idx_lat] = ismember(india_lat,all_lat);
[~,idx_lon] = ismember(india_lon,all_lon);
% set those elements of X to the values in Y:
X(idx_lat,idx_lon) = Y;
% plot to see the result
s = pcolor(all_lon,all_lat,X);
set(s,'EdgeColor','none');
  댓글 수: 2
Aarti Soni
Aarti Soni 2022년 4월 17일
@_ thanks for the code but I am getting values at this position.
Voss
Voss 2022년 4월 18일
One difference is that I used all_lon and all_lat as the x and y coordinates for my pcolor plot and you are using indices. Also, the 'YDir' of your axes is 'reverse'.
If I make those two changes I get your plot:
all_lat = -89.75:0.25:90;
% according to the longitudes specified for India (66.5 to 100),
% longitude goes from -180 to 180, not 0 to 360:
all_lon = (0.25:0.25:360)-180;
india_lat = 6.5:0.25:38.5;
india_lon = 66.5:0.25:100.0;
whos *_lat *_lon
Name Size Bytes Class Attributes all_lat 1x720 5760 double all_lon 1x1440 11520 double india_lat 1x129 1032 double india_lon 1x135 1080 double
X = zeros(numel(all_lat),numel(all_lon)); % your zeros matrix
Y = rand(numel(india_lat),numel(india_lon)); % random matrix substituting for your Y matrix
% determine where Y belongs in X:
[~,idx_lat] = ismember(india_lat,all_lat);
[~,idx_lon] = ismember(india_lon,all_lon);
% set those elements of X to the values in Y:
X(idx_lat,idx_lon) = Y;
% plot to see the result
% s = pcolor(all_lon,all_lat,X); % using all_lon and all_lat for x and y in pcolor
s = pcolor(X); % using 1:size(X,2), 1:size(X,1)
set(s,'EdgeColor','none');
set(gca(),'YDir','reverse'); % reverse the Y axis

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by