Number of elements mismatch.

5 ビュー (過去 30 日間)
Mike Leijten
Mike Leijten 2021 年 10 月 12 日
コメント済み: Dave B 2021 年 10 月 12 日
Hi everyone,
I have a dimension issue:
I have a Simulink Matlab Function block that has a [3x1] vector of values called pd as its input. This vector should turn into a [6x1] vector called pdot by adding three zeros to every element. Basically just resizing
from to it sounds rather simple but I can't get it to work. The code that I use in my Simulink function block is shown below.
function pdot = fcn(pd)
pdot = zeros(6,1);
temp = zeros(6,1);
for i = 1:length(pd)
pdot(i) = temp(i) + [pd(i); 0; 0; 0];
end
end
Thanks in advance for any tips or advice.

採用された回答

Dave B
Dave B 2021 年 10 月 12 日
This seems like you maybe over complicated it? Is it as simple as...
x=[1;3;5]
x = 3×1
1 3 5
fcn(x)
ans = 6×1
1 3 5 0 0 0
function pdot = fcn(pd)
pdot=[pd;0;0;0];
end
(note that padarray can also be used to do this)
  2 件のコメント
Mike Leijten
Mike Leijten 2021 年 10 月 12 日
Thanks. I have a follow up question if you don't mind:
pd is 3x1x51 in the workspace (51 are the timesteps in Simulink) and pdot is 51x6. How can I get pdot to look like 6x1x51?
Dave B
Dave B 2021 年 10 月 12 日
Happy to help.
Two options:
Take the 51 x 6 and make it 6 x 1 x 51:
pdot=rand(51,6);
pdot=reshape(pdot.',6,1,51); % transpose then reshape
Use padarray to add the zeros directly to the 3 x 1 x 51:
pd = rand(3,1,51);
pdot = padarray(pd, [3 0 0], 0, 'post'); % pad with 3 rows (0 columns, 0 in third dimension), with the value 0, padding on the 'bottom' not the 'top'

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeString についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by