Store sets of values as indices in a matrix?

1 回表示 (過去 30 日間)
BOB
BOB 2018 年 9 月 10 日
コメント済み: BOB 2018 年 9 月 10 日
Hi,
I have a function which produces a set of horizontal and vertical graph axes intercepts. There are 120 sets of these intercepts:
for Z = 1:120
hrz = hix(x(:,Z),mb(:,Z))'; %[X Y] Matrix of horizontal intercepts
vrt = vix(y(:,Z),mb(:,Z))'; %[X Y] Matrix of vertical intercepts
end
I want to store each of these sets of intercepts in a matrix, i.e. I want to be able to call the second set of intercepts by simply calling matrix(2).
Is anyone aware of a way to achieve this?
Thanks

採用された回答

Guillaume
Guillaume 2018 年 9 月 10 日
I'm assuming that hix and vix are functions, and I'm assuming that for each pair of vector input, they each return a vector of the same size as the input. In which case,
hrz = zeros(size(x)); %predeclare output matrix
vrt = zeros(size(x));
for Z = 1:120
hrz(:, Z) = hix(x(:, Z), mb(:, Z));
vrt(:, Z) = vix(y(:, Z), mb(:, Z));
end
  6 件のコメント
Guillaume
Guillaume 2018 年 9 月 10 日
store the different length vector of each iteration of the loop in a single matrix.
Ah! That needed explaining in the question. By definition, a matrix has the same number of rows/columns for each column/row. So you can't store results of varying length in a matrix unless you pad the shorter ones (which is rarely a good approach).
You can store the results in a cell array though:
hrz = cell(1, size(x, 2)); %preallocate output
vrt = cell(1, size(x, 2));
for Z = 1:size(x, 2) %better not to hardcode the size
hrz{Z} = hix(x(:, Z), mb(:, Z));
vrt{Z} = vix(y(:, Z), mb(:, Z));
end
BOB
BOB 2018 年 9 月 10 日
That works perfectly! Thanks for the persistence, I'll make sure to be more clear next time.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeModify Image Colors についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by