Replace the value in the columns of a matrix

1 回表示 (過去 30 日間)
Mario Marinov
Mario Marinov 2015 年 9 月 22 日
コメント済み: Mario Marinov 2015 年 9 月 22 日
In matrix A = [-1 2 3; 4 -5 6; 7 8 -9] order for k1 = 1: size (A, 1) A (k1, find (A (k1,:)> 0, 1, 'first')) = 1 makes replacement of the first positive value in each row 1. If it changes the expression so: for k1 = 1: size (A, 1) A (k1, find (A (k1,:)> 0, 1, 'last')) = 1 will replaced the last positive value in each row. How to do so, to replace the last positive value of 1, but in each column?
  2 件のコメント
the cyclist
the cyclist 2015 年 9 月 22 日
Is this a homework problem? It seems a bit hard to believe that you could write the code for doing rows, but not understand how to do it for columns.
Mario Marinov
Mario Marinov 2015 年 9 月 22 日
Not quite so, but thanks!

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

回答 (1 件)

Kirby Fears
Kirby Fears 2015 年 9 月 22 日
You can switch the loop to iterate over the columns instead of the rows by changing
k1=1:size(A,1)
to
k1=1:size(A,2)
Then switch the indexing of A similarly, changing
A(k1, find (A (k1,:)> 0, 1, 'last'))
to
A(find(A(:,k1)> 0, 1, 'last'),k1)
  1 件のコメント
Mario Marinov
Mario Marinov 2015 年 9 月 22 日
Thank you so much! Work and understand why!

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

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by