Subset of a matrix

3 ビュー (過去 30 日間)
Stéphane
Stéphane 2014 年 11 月 13 日
コメント済み: Guillaume 2014 年 11 月 13 日
Hi,
This question is a bit tricky to explain with words, so I'll write the matlab code I would like...
Starting with : x = 1:5; y = (-5:5)'; iPos = y > 0;
I would like to write something like :
z = [y*x](iPos);
Which would be the equivalent of
z = y*x; z = z(iPos);
without creating the z matrix. I do it with Python but I can't figure out the way to do it in Matlab... Is it possible ?
Thanks

採用された回答

Guillaume
Guillaume 2014 年 11 月 13 日
Did you mean
z(:, iPos)
as it's a bit unusual to use a logical index as a linear index when it's smaller than the number of elements in the matrix.
To answer your question, unfortunately, you can't index into temporaries in matlab, be it the result of an expression as in your example, or the return value of function. You always have to create a variable to store the result and there is no trick that can work around that.
However, in your case, you could do
z = y(iPos) * x(1);
Or if you meant to get all rows:
z = y(iPos) * x;
  2 件のコメント
Stéphane
Stéphane 2014 年 11 月 13 日
Thanks for your answer.
I actually have large 2D matrices and I apply different operations depending on conditions in both direction, so the way I found to do it is to apply binary masks.
By the way, I'm using these temporaries because I think it saves memory when using large matrices but I'm not sure about that...
Am I correct ?
Guillaume
Guillaume 2014 年 11 月 13 日
Can you show a more realistic example?
Note that even if matlab allowed
z = [y*x](iPos);
it would still use the same amount of memory.

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

その他の回答 (0 件)

カテゴリ

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