Hello everyone. I have a 257x1 double that I need to add zero values to at set locations without removing the pre-existing data, in essence expanding the double to a 264x1.
I've been searching through existing answers but am still not quite getting it due to most cases involving NxM matrices as opposed to having a single column.
Thanks for your help Conor
EDIT -------------- After some additional searching I found a method that solves my problem but I am still curious if it is the most efficient, here is my finished code:
U = ones (264,1);
U (:) = NaN;
U ([9;37;38;39;49;50;51]) = 0;
URLocations = find (isnan (U));
U (URLocations) = UR;

 採用された回答

Stephen23
Stephen23 2017 年 5 月 5 日
編集済み: Stephen23 2017 年 5 月 5 日

0 投票

Simplified a little:
U = nan(264,1);
U([9;37;38;39;49;50;51]) = 0;
U(isnan(U)) = UR;
  • find is not required and is just slows the code down. Logical indexing is faster.
  • there is no point creating a matrix of ones when you want a matrix of NaNs.
  • allocating the index variable just clutters the workspace and slows the code.
An alternative ( I have no idea if this will be faster, you should time them with timeit ):
U = zeros(264,1);
U(setdiff(1:264,[9;37;38;39;49;50;51]) = UR;

1 件のコメント

Conor J Crickmore
Conor J Crickmore 2017 年 5 月 5 日
Thank you very much, explains why you did what you did too :-)

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by