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

采纳的回答

Walter Roberson
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;
  2 个评论
Adel Gohari
Adel Gohari 2019-11-5
many thanks for your smart answer. I've never looked to the issue from the point of view you've mentioned.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by