How to set up a program that introduces a square matrix of numbers from the fourth line? The program should be replaced first positive element in each row 1 and outputting the resulting matrix. Go somewhere and ... Please help!

2 件のコメント

James Tursa
James Tursa 2015 年 9 月 16 日
Please provide a short example of inputs and desired outputs.
Mario Marinov
Mario Marinov 2015 年 9 月 16 日
Let matrix is >> A=[1 2 3;4 5 6;7 8 9]
A =
1 2 3
4 5 6
7 8 9
The program to replace the first numbers in each row, if they are positive, with the number 1. Then put the newly matrix :)

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

 採用された回答

Star Strider
Star Strider 2015 年 9 月 16 日

0 投票

I’m not certain what you want. See if this works:
A=[1 2 3;4 5 6;7 8 9];
A(A(1,:)>0) = 1;
It tests the first column in every row, and if it is positive, replaces it with 1.

2 件のコメント

Mario Marinov
Mario Marinov 2015 年 9 月 16 日
編集済み: Star Strider 2015 年 9 月 16 日
This works! Thanks! My record is wrong:
>> A(1,:)>0=1
A(1,:)>0=1
|
Error: The expression to the left of the equals sign is not a valid target for an assignment.
Star Strider
Star Strider 2015 年 9 月 16 日
You have to put parentheses in the subscript references:
A(A(1,:)>0) = 1;

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

その他の回答 (1 件)

Mario Marinov
Mario Marinov 2015 年 9 月 16 日

0 投票

Excuse me, but if the matrix is this: >> A = [-1 2 3;4 -5 6;7 8 -9]
A =
-1 2 3
4 -5 6
7 8 -9
How to replace the first positive number in each row, and not in the first column with 1?

3 件のコメント

Star Strider
Star Strider 2015 年 9 月 16 日
With those conditions, the code requires a loop and the find function:
A = [-1 2 3;4 -5 6;7 8 -9];
for k1 = 1:size(A,1)
A(k1,find(A(k1,:)>0, 1, 'first')) = 1;
end
Mario Marinov
Mario Marinov 2015 年 9 月 16 日
Working again! Thanks!
Star Strider
Star Strider 2015 年 9 月 16 日
My pleasure!

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

カテゴリ

ヘルプ センター および File ExchangeMatrices and Arrays についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by