reading excel data in for loop
1 次查看(过去 30 天)
显示 更早的评论
Hi. I intend to assign the value of each cell of TT(x,y) inside the for loop.
A: is a 501*584 matrics including zero and non-zero values.
solid is a 501*584 matrics including 0 and 1 value (0 and 1 indicate the liquid and solid area, respectively).
Lx is the number of rows (501), and Ly is the number of columns (584).
but when the code is run, the "subscripted assignement dimension mismatch" error message related to the line number 5 was appeared. Highly appreciate any solution for this problem.
1- T = xlsread ('A');
2- for y=1:Ly
3- for x=1:Lx
4- if solid (x,y)==0
5- TT(x,y)=T;
6- else
7- TT(x,y)=0;
8- end
9- end
0 个评论
采纳的回答
Walter Roberson
2019-11-4
TT(x,y) = T(x,y);
You are currently trying to assign all of T into the specific location TT(x,y)
Note: if I am corect that you want T(x,y) then your code can be made much more compact, with no for loops at all:
TT = T .* (solid == 0);
When solid is non-zero then solid==0 is false, which is 0, and 0 times anything finite is 0 is the result of the expression.
When solid is 0 then solid==0 is true, which is 1, and 1 times anything numeric is the value itself.
No loops needed.
Note: if some T values might be infinite where solid is not zero then 0*infinity is nan instead of being 0, so if that is a realistic case, you should consider
TT((solid == 0) & isnan(TT)) = 0;
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!