【问题标题】:Setting a matrix to one on a list of incomplete rectangular grid points在不完整的矩形网格点列表上将矩阵设置为一个
【发布时间】:2014-06-09 22:02:33
【问题描述】:

我在一个文本文件中有一组数据,其中显示了 81x61 大小的矩形网格的坐标。每行显示一个网格点的经度和纬度。经度从 50.00 变为 80.00(61 个值),纬度从 30.00 变为 70.00(81 个值),顺序如下:

50.00  30.00
51.50  30.00
52.00  30.00
.
.
.
79.50  30.00
80.00  30.00
50.00  31.00
50.50  31.00
51.00  31.00
.
.
.
79.00  70.00
79.50  70.00
80.00  70.00

我还有另一个文本文件,其中包含一些来自上述矩形网格的随机坐标。

我想创建一个大小为 81x61 的矩阵,其元素为 0 和 1,其中 1 对应于第二个文本文件中的坐标。

如何在 Matlab 中编写代码?


我需要的小规模示例:

文本文件:

  1  1
  1  2
  1  3
  .
  .
  .
  4  3
  4  4
  4  5 

上述文本文件对应的矩形网格:

1,1  1,2  1,3  1,4  1,5
2,1  2,2  2,3  2,4  2,5
3,1  3,2  3,3  3,4  3,5
4,1  4,2  4,3  4,4  4,5

第二个文本文件:

1  1
1  3
2  4
2  5
3  4
4  1
4  5  

上述文本文件对应的矩阵:

1  0  1  0  0
0  0  0  1  1
0  0  0  1  0
1  0  0  0  1

【问题讨论】:

  • 您对所需结果的描述有点模糊,您能否提供一个示例,其中包含一组(缩短的)输入/文本文件和所需的输出?
  • @timgeb:我编辑了问题,希望对您有所帮助
  • 我不明白第一个文本文件的重要性。您始终可以从第二个文本文件中读取并获取哪些坐标存在,哪些不存在
  • @Nishant:如果第二个文件缺少像第 4 行这样的行的坐标,我怎么知道我的矩阵应该有 4 行?
  • 是否没有预先声明您的第一个文本文件中的值将在50:8030:70 范围内,因此您的矩阵大小为81x61

标签: matlab matrix coordinates


【解决方案1】:

我自己找到了方法;

假设:经度值最小值为 50.00,纬度值最小值为 30.00

%// First column of the files is Longitude and the second column is Latitude
  fr = fopen('second_file.txt','r');
  lon = textscan(fr,'%f %*[^\n]');
  lon = lon{:};
  fclose(fr);
  fr = fopen('second_file.txt','r');
  lat = textscan(fr,'%*f %f %*[^\n]');
  lat = lat{:};
  fclose(fr);
%// We know that the overall size of the target matrix is 81x61
  overall = zeros(81,61);
%// We assume that the total number of lines in the second file is 1000 (don't know if there is a built-in command to determine that!)
  for k = 1:1000
      i = int16(( lat(k) - 30.00 ) / 0.5 + 1);
      j = int16(( lon(k) - 50.00 ) / 0.5 + 1);
      overall(i,j) = 1;
  end

【讨论】:

    【解决方案2】:

    我想创建一个大小为 81x61 的矩阵,其中元素为 0 和 1 1s 对应于第二个文本文件中的坐标的方式。

    这就是问题中的相关信息。答案是 Matlab 函数sub2ind(documentation)。它将 x、y 坐标列表转换为数组索引列表,然后您可以方便地将其设置为一个。

    假设您已读取名为 second_file 的 Nx2 矩阵中的第二个文件的内容,以及您在变量 matrix_size (81x61) 中给出的结果矩阵的大小。然后你做:

    x = second_file(:, 1);
    y = second_file(:, 2);
    result = zeros(matrix_size);
    index = sub2ind(matrix_size, x, y);
    result(index) = 1;
    

    【讨论】:

    • 我认为您忘记将第二个矩阵中的值转换为索引,因为它们分别是范围 50:80 and 30:70 和 cols 1 and 2 的双精度值。
    【解决方案3】:

    我假设您的第一个文件中各列中的minimummaximum 值是纬度和经度的范围。此外,在这两个文件中,第一列都是经度值。

     %// calculating the min and max of latitude and longitude 
     id1  = fopen('first_file.txt');
     A = fscanf(id1,'%f %f',[2 Inf]); 
     long_min = min(A(1,:));
     long_max = max(A(1,:));
     lat_min = min(A(2,:));
     lat_max = max(A(2,:));
     %// calculating output matrix size
     no_row = (lat_max-lat_min)*2+1;   
     no_col = (long_max-long_min)*2+1;
     output = zeros(no_row,no_col);
    
     id2  = fopen('second_file.txt');
     A = fscanf(id2,'%f %f',[2 Inf]);
     % // converting the values into corresponding indices
     A(1,:) = A(1,:) - long_min;     
     A(2,:) = A(2,:) - lat_min;   
     A = A*2 +1; 
     linear_ind = sub2ind([no_row no_col],A(2,:),A(1,:));
     output(linear_ind) = 1;
    

    第二种方法 我假设在您的第二个文本文件中,第一列条目是纬度,第二列条目是经度。您必须对以下变量进行硬编码:

     long_min : the minimum value among longitude values
     lat_min : the minimum value among latitude values
     long_max : maximum value among longitude values
     lat_max : maximum value among latitude values
    

    这是代码(仅考虑第二个文本文件)

     no_row = (lat_max-lat_min)*2+1;   
     no_col = (long_max-long_min)*2+1;
     output = zeros(no_row,no_col);
     id2  = fopen('second_file.txt');
     A = fscanf(id2,'%f %f',[2 Inf]);
     % // converting the values into corresponding indices
     A(1,:) = A(1,:) - long_min;     
     A(2,:) = A(2,:) - lat_min;   
     A = A*2 +1; 
     linear_ind = sub2ind([no_row no_col],A(2,:),A(1,:));
     output(linear_ind) = 1;
    

    【讨论】:

    • @Nishat:谢谢,但您的解决方案不起作用。正如您所定义的,A 的大小为 2x1,只有两个值!我认为你应该改变 A 的定义方式。
    • @Nishat:我猜到了你的意图,但最后它只创建了一个全零值的矩阵 111x127!
    • @gnome 您能否确认在您的两个文本文件中,一行中的值仅由一个空格分隔,两个连续行由一个换行符分隔。
    • 每行的值用制表符分隔,连续两行用换行符分隔
    • @gnome 查看编辑后的代码,请注意我的假设
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-15
    • 2018-12-11
    • 1970-01-01
    • 2019-04-21
    • 2013-05-17
    • 1970-01-01
    相关资源
    最近更新 更多