Error: Subscripted assignment dimension mismatch.

Trying to assign a new 3-component(ijk) velocity vector of the form [vi(1),vj(1),vk(1)] to each satellite for 1000 satellites and can't get past the error in the title...
I would expect it to be like so...
v(1)=[1.2333,0.3444,0.2334]
v(2)=[2.0112,-0.1223,-.9890]
...
numsat=1000;
sig=1;
mu=0;
vi = normrnd(mu,sig,[1 numsat]); % km/s
vj = normrnd(mu,sig,[1 numsat]); % km/s
vk = normrnd(mu,sig,[1 numsat]); % km/s
% Establish position in ijk
% This is done at the north pole for convenience sake
alt = 800; % altitude in km
pi = 0; % km
pj = 0; % km
pk = alt+6378.1363; % alt plus radius of earth
for i = 1:numsat
v(i)=[vi(i),vj(i),vk(i)]
end

 採用された回答

Jan
Jan 2017 年 8 月 22 日

1 投票

In
v(i)=[vi(i),vj(i),vk(i)]
you try to assign a [1 x 3] vector to the scalar v(i). This cannot work.
You do not need a loop for a concatenation:
v = [v.', vj.', vk.'];
now v(i, :) is a [1 x 3] vector.

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2017 年 8 月 22 日
編集済み: Walter Roberson 2017 年 8 月 22 日

0 投票

v(i) is indexing notation for a single entry in an array -- either a single scalar in a numeric array, or a single cell in a cell array.
[vi(i),vj(i),vk(i)] is notation that takes three values and puts them into a row vector, giving you a 1 x 3 array of results. The data type of [vi(i),vj(i),vk(i)] will be cell if any of the three values are cells; the data type will be char if any of the three values are char and the rest are char or numeric; the data type will be logical if all of vi(i), vj(i), vk(i) are logical; the data type will be numeric if at least one of the values is numeric and the others are numeric or logical. Cases combining char and logical are errors unless the third value is cell.
So [vi(i),vj(i),vk(i)] will be a 1 x 3 array. And you are trying to assign that to v(i) which only has room for a single value.
You have three choices: you can go for
v(1,:)=[1.2333,0.3444,0.2334]
v(2,:)=[2.0112,-0.1223,-.9890]
which the first index is used to select a row of values, and you would need to use a notation such as v(i,:) to extract the values.
Or you can go for
v{1}=[1.2333,0.3444,0.2334]
v{2}=[2.0112,-0.1223,-.9890]
where you use cell arrays, and you would use notation such as v{i} to extract the values;
Or you can go for
v(1).values = [1.2333,0.3444,0.2334]
v(2).values = [2.0112,-0.1223,-.9890]
where you use a structure array, and you would use notation such as v(i).values to extract the values.
The first of these is a lot more efficient for memory and calculation speed, and should be your first choice if your components have consistent size for each entry.

カテゴリ

ヘルプ センター および File ExchangeReference Applications についてさらに検索

タグ

質問済み:

2017 年 8 月 22 日

編集済み:

2017 年 8 月 22 日

Community Treasure Hunt

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

Start Hunting!

Translated by