reading excel data in for loop

11 ビュー (過去 30 日間)
Adel Gohari
Adel Gohari 2019 年 11 月 4 日
コメント済み: Adel Gohari 2019 年 11 月 5 日
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 件のコメント
KALYAN ACHARJYA
KALYAN ACHARJYA 2019 年 11 月 4 日
Yes sir
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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by