Finite Element: Filling nxm matrix by extracting values from another nx1 matrix

1 回表示 (過去 30 日間)
Suhaila Al-Ali
Suhaila Al-Ali 2022 年 12 月 2 日
回答済み: Fifteen12 2022 年 12 月 2 日
I need a loop code or a function which can fill an nxm matrix by extracting values from another nx1 matrix.
Below is an example of a known matrix size:
V(2,2:5)=W(1:4);
V(3,2:5)=W(5:8);
V(4,2:5)=W(9:12);
V(5,2:5)=W(13:16);
V is a matrix of 6x6 and W is a matrix of 16x1.

回答 (2 件)

Voss
Voss 2022 年 12 月 2 日
V(2:5,2:5) = reshape(W,4,[]).';

Fifteen12
Fifteen12 2022 年 12 月 2 日
From your question, V is mxm and W is nx1. Assuming that's true going forward:
n = 16;
m = 4;
offset = 1;
W = randi(10, [n, 1]); %initialize W
V = zeros(6);
disp(myfunc(V, offset, m, W));
0 0 0 0 0 0 0 8 3 5 6 0 0 5 9 1 1 0 0 5 4 10 8 0 0 2 6 4 9 0 0 0 0 0 0 0
function V = myfunc(V, offset, m, W)
n = length(W);
numRows = floor(n/m);
for i = 1:numRows
V(offset+i, offset+1:m+offset) = W((i-1)*m+1:i*m);
end
end

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by