我试图通过特定的风暴ID找到最小压力并将其赋值为1。我尝试使用带有if语句的嵌套for循环,但这不起作用。下面是我的代码,如果你能帮忙,那就太好了!
lifecycle = zeros(285,1); %// variable lifecycle denotes max storm intensity
for c = 1:285 %// counter
for id = 188:100:1288 %// loop through each storm code(188,288,...1288)
if min_press(c) == min(min_press(id)) %// find min pressure of each id
lifecycle(c) = 1; %// assign min a value of 1
end
end
end
答案 0 :(得分:1)
假设您的min_press
变量是一个二维矩阵,其最小压力表示为列向量,您可以这样做。
A
是你的压力值
A= randn(3,3)
-0.063413 -2.130337 0.590931
0.233517 -0.112800 -0.898581
-0.395259 0.303704 2.508438
min()以逐列为基础工作,因此获取矩阵的最小值将为每列提供最小值
[val, row] = min(A)
val =
-0.39526 -2.13034 -0.89858
row =
3 1 2
将这些转换为与矩阵A
ind = sub2ind(size(A),row.', [1:length(row)].')
ind =
3
4
8
将它们应用于A
A(ind) = 0
A =
-0.06341 0.00000 0.59093
0.23352 -0.11280 0.00000
0.00000 0.30370 2.50844