用于获得随机数之和的原始数的Matlab算法

时间:2017-02-12 12:59:45

标签: matlab

在一个坐在圆桌旁的房间里有8个人,每个人都选择一个随机数,并与他左右两侧的人数相加。结果是按照人们在桌子上的顺序带给你的,你如何获得每个人选择的原始数字?

Lower_rand_limit = 1;
Upper_rand_Limit = 100;
Number_of_colums = 8;
Number_of_lines = 1;
Random_matrix = randi([Lower_rand_limit Upper_rand_Limit], Number_of_lines, Number_of_colums)
Sum_matrix = zeros(Number_of_lines, Number_of_colums);

for i = 1:Number_of_colums
    if i == 1
        Sum_matrix(Number_of_lines, i) = Random_matrix(Number_of_lines, Number_of_colums) + Random_matrix(Number_of_lines, i) + Random_matrix(Number_of_lines, i + 1);
    elseif i == Number_of_colums
        Sum_matrix(Number_of_lines, i) = Random_matrix(Number_of_lines, Number_of_colums- 1) + Random_matrix(Number_of_lines, Number_of_colums) + Random_matrix(Number_of_lines, 1);
    else
        Sum_matrix(Number_of_lines, i) = Random_matrix(Number_of_lines, i - 1) + Random_matrix(Number_of_lines, i) + Random_matrix(Number_of_lines, i + 1);
    end
end

Sum_matrix

现在只有Sum_matrix才能获得原始的Randon_matrix?

感谢nikaltipar的安装,它现在正在工作,因为谁想要查看整个代码,他就在这里:

Lower_rand_limit = 1;
Upper_rand_Limit = 100;
Number_of_colums = 1;
Number_of_lines = 8;
Random_matrix = randi([Lower_rand_limit Upper_rand_Limit], Number_of_lines, Number_of_colums)
Sum_matrix = zeros(Number_of_lines, Number_of_colums);

 for i = 1:Number_of_lines
     if i == 1
         Sum_matrix(i, 1) = Random_matrix(Number_of_lines, 1) + Random_matrix(i, 1) + Random_matrix(i + 1, 1);
     elseif i == Number_of_lines
         Sum_matrix(i, 1) = Random_matrix(Number_of_lines - 1, 1) + Random_matrix(Number_of_lines, 1) + Random_matrix(1, 1);
     else
         Sum_matrix(i, 1) = Random_matrix(i - 1, 1) + Random_matrix(i, 1) + Random_matrix(i + 1, 1);
     end
 end

Sum_matrix

A = [1 1 0 0 0 0 0 1;
     1 1 1 0 0 0 0 0;
     0 1 1 1 0 0 0 0;
     0 0 1 1 1 0 0 0;
     0 0 0 1 1 1 0 0;
     0 0 0 0 1 1 1 0;
     0 0 0 0 0 1 1 1;
     1 0 0 0 0 0 1 1];

Result_matrix = A\Sum_matrix;

Result_matrix

1 个答案:

答案 0 :(得分:1)

这样的东西?

is

您提供string input; std::getline(std::cin, input); 这是一个列向量,其中包含您所描述的总和,输出function [ x ] = original_numbers( b ) A = [1 1 0 0 0 0 0 1; 1 1 1 0 0 0 0 0; 0 1 1 1 0 0 0 0; 0 0 1 1 1 0 0 0; 0 0 0 1 1 1 0 0; 0 0 0 0 1 1 1 0; 0 0 0 0 0 1 1 1; 1 0 0 0 0 0 1 1]; x = A\b; end 包含表中人员提供的原始数字。

e.g

b