我正在尝试更改以下代码,以便第一个矩阵成为第二个矩阵:
function BellTri = matrix(n)
BellTri = zeros(n);
BellTri(1,1) = 1;
for i = 2:n
BellTri(i,1) = BellTri(i-1,i-1);
for j = 2:i
BellTri(i,j) = BellTri(i - 1,j-1) + BellTri(i,j-1);
end
end
BellTri
第一个矩阵(当n = 7时)
1 0 0 0 0 0 0
1 2 0 0 0 0 0
2 3 5 0 0 0 0
5 7 10 15 0 0 0
15 20 27 37 52 0 0
52 67 87 114 151 203 0
203 255 322 409 523 674 877
第二个矩阵
1 1 2 5 15 52 877
1 3 10 37 151 674 0
2 7 27 114 523 0 0
5 20 87 409 0 0 0
15 67 322 0 0 0 0
52 255 0 0 0 0 0
203 0 0 0 0 0 0
答案 0 :(得分:1)
一种方法:
out = zeros(size(A));
out(logical(fliplr(triu(ones(size(A,1)))))) = A(logical(tril(ones(size(A,1)))));
注意:正如Divakar指出的那样,第一行应该有一个拼写错误。这种方法给出了纠正的方法。
<强>结果:强>
A = [1 0 0 0 0 0 0;
1 2 0 0 0 0 0;
2 3 5 0 0 0 0;
5 7 10 15 0 0 0;
15 20 27 37 52 0 0;
52 67 87 114 151 203 0;
203 255 322 409 523 674 877];
>> out
out =
1 2 5 15 52 203 877
1 3 10 37 151 674 0
2 7 27 114 523 0 0
5 20 87 409 0 0 0
15 67 322 0 0 0 0
52 255 0 0 0 0 0
203 0 0 0 0 0 0
答案 1 :(得分:1)
选项是使用#include <iostream>
#include <string>
#include <fstream>
#include <deque>
using namespace std;
string findGroupKey(const string &line)
{
size_t first = line.find('_');
if (first == string::npos)
first = 0;
size_t last = line.find_last_of('.');
size_t len = (last == string::npos ? string::npos : last - first + 1);
// The key includes the start and end chars themselves in order to
// distinguish lines like "x_test." and "xtest"
return line.substr(first,len);
}
int main()
{
// *** Var defs
// Read the input file as stream
ifstream inStream("test.txt");
// line by line placing each just read line into inLine
string inLine;
// Place each inLine into its one group
deque<deque<string> *> linesGrouped;
// according to the grouping key
deque<string> keys;
// *** Read the input file and group the lines in memory collections
while (getline(inStream, inLine)) {
string groupKey = findGroupKey(inLine);
// Find the key in our keys-met-so-far collection
int keyIndex = -1;
for (int i = 0, keyCount = (int)keys.size(); i < keyCount; i++)
if (keys.at(i) == groupKey) {
keyIndex = i;
break;
};
if (keyIndex == -1) {
// If haven't found the key so far, add it to our key index collection
keys.push_back(groupKey);
// and add a new group collection
deque<string> *newGroup = new deque<string>();
newGroup->push_back(inLine);
linesGrouped.push_back(newGroup);
} else {
// Otherwise just add the line into its respective group
linesGrouped.at(keyIndex)->push_back(inLine);
}
}
// *** Write the groups into the output file
ofstream outStream("output.txt");
for (int i = 0, groupCount = (int)linesGrouped.size(); i < groupCount; i++) {
for (int j = 0, lineCount = (int)linesGrouped.at(i)->size(); j < lineCount; j++)
outStream << linesGrouped.at(i)->at(j) << endl;
// Add a delimiter line (uncomment if you need one)
//if (i < groupCount - 1)
// outStream << "-------------------" << endl;
}
return 0;
}
循环置换列。
circshift
输入:function [BellTri, Second] = matrix(n)
BellTri = zeros(n);
BellTri(1,1) = 1;
for i = 2:n
BellTri(i,1) = BellTri(i-1,i-1);
for j = 2:i
BellTri(i,j) = BellTri(i - 1,j-1) + BellTri(i,j-1);
end
end
Second = BellTri;
for i = 1:n
Second(:, i) = circshift(Second(:,i), 1-i);
end
for i = n-1:-1:2
Second(1, i) = Second(1, i-1);
end
end
输出:
[BellTri, Second] = matrix(7)