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 个)

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 CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by