How to index a matrix to make it from a vector, taking some elements from the vector and put them in a new row?
1 次查看(过去 30 天)
显示 更早的评论
I need to construct a matrix taking three elements from a 14-elements vector v(1:3) and write them in a row, then take the next three elements v(2:4) in the next row, and so on. Such that that I need to have something as in the image:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1211563/image.jpeg)
I have this piece of code but the indexing is no correct:
pilots4356 = Rxc1(43:56); %vector
matrix4356 = zeros(12,14); %matrix size
pospilot = 1; %index
for f = 1:12 %rows
for c = 1:14 %columns
for pospilot = pospilot:pospilot+2
matrix4356(:,c) = pilots4356(pospilot);
end
pospilot = pospilot+1;
end
end
matrix4356
0 个评论
采纳的回答
Voss
2022-11-28
Here's one way to do it:
pilots4356 = 43:56; %vector
N = numel(pilots4356)-2;
matrix4356 = zeros(N,N+2); %matrix size
% place the vector along the diagonals of the matrix:
matrix4356(1:N+1:N^2) = pilots4356(1:end-2);
matrix4356(N+1:N+1:N^2+N) = pilots4356(2:end-1);
matrix4356(2*N+1:N+1:N^2+2*N) = pilots4356(3:end);
disp(matrix4356);
0 个评论
更多回答(1 个)
Stephen23
2022-11-29
A general approach:
V = 43:56;
N = 3;
C = numel(V);
M = V .* tril(triu(ones(C-N+1,C)),N-1);
disp(M)
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!