If statement without loop

14 ビュー (過去 30 日間)
Hokkien
Hokkien 2021 年 12 月 27 日
コメント済み: Hokkien 2021 年 12 月 27 日
Hi there are two questions I wish to ask:
First, I would like to use if else statement without the loop. Here is my attempt with the loop:
for k=1:15
for j=1:10
if A(j,k) > B(j,1), C(j,k)=0;
else
C(j,k)=A(j,k);
end
end
end
toc;
This took some computation time but I wish to cutdown the time. I attempt to use:
TR1_Ge70(TR>TR_Ge&)=0;
However, the matrix dimension is incorrect. Here is the example:
Matrix A Matrix B Matrix C
1 2 3 4 5 3 1 2 0 0 0
1 2 3 4 5 2 1 0 0 0 0
1 2 3 4 5 1 0 0 0 0 0
1 2 3 4 5 5 1 2 3 4 0
The second question will be bit massive and I fail to write one. I have a matrix A, let say 3 by 3. I also have matrix B, let say 3 by 3 as well. I wish to multiple the first column of matrix A with the entire matrix B, and the scond column and etc. For example:
Matrix A Matrix B Outcome using first column A: Outcome using second column B:
1 2 2 2 2 1 2 2 1 4 4 2
3 3 2 3 3 2 9 9 6 9 9 6
4 2 3 1 2 1 4 8 4 2 4 2
Based on this, I will get three set outcomes. However, it's seem like I cannot write the code and give me three different outcomes directly. The only way that I think is that I have to maintain only single column of Matrix A, and change it every time in order to get three different outcomes. Is it a quick way to do this? Thank you so much for the help!
  2 件のコメント
DGM
DGM 2021 年 12 月 27 日
What version are you using?
Hokkien
Hokkien 2021 年 12 月 27 日
Hi I'm using Matlab R2020b

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

採用された回答

Stephen23
Stephen23 2021 年 12 月 27 日
編集済み: Stephen23 2021 年 12 月 27 日
A = repmat(1:5,4,1)
A = 4×5
1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5
B = [3;2;1;5]
B = 4×1
3 2 1 5
C = A;
C((1:5)>=B) = 0
C = 4×5
1 2 0 0 0 1 0 0 0 0 0 0 0 0 0 1 2 3 4 0
  1 件のコメント
Hokkien
Hokkien 2021 年 12 月 27 日
Thank you so much! I understand now!

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

その他の回答 (1 件)

David Hill
David Hill 2021 年 12 月 27 日
TR1_Ge70(TR1>TR1_Ge70(:,1))=0;
for k=1:size(A,2)
C(:,:,k)=A(:,k).*B;
end
  1 件のコメント
Hokkien
Hokkien 2021 年 12 月 27 日
Hi Thank you so much, unfornutately I still have dimension problem. I'm sorry if my question is not clear. I have re-edit it with the example:
Matrix A Matrix B Matrix C
1 2 3 4 5 3 1 2 0 0 0
1 2 3 4 5 2 1 0 0 0 0
1 2 3 4 5 1 0 0 0 0 0
1 2 3 4 5 5 1 2 3 4 0
Thank you!

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by