gpt4 book ai didi

arrays - 替换索引后的数组项

转载 作者:行者123 更新时间:2023-12-02 01:26:33 25 4
gpt4 key购买 nike

A=[x1,x2,x3,y1,y2,y3,y4]

我想替换数组 Ay1 之后的所有内容:

A=[x1,x2,x3,y1,y1,y1,y1]

我试过了

for i in eachindex(A)
if A[i]==y1
for j in i:length(A)
A[j]=y1
end
end
end

但是看起来很复杂,有没有其他的方法可以简单点呢?

最佳答案

这是你想要的吗?在示例中,我假设您的 y11

julia> A = [5, 4, 3, 1, 2, 7, 8]
7-element Vector{Int64}:
5
4
3
1
2
7
8

julia> loc = findfirst(==(1), A)
4

julia> isnothing(A) || (A[loc:end] .= 1);

julia> A
7-element Vector{Int64}:
5
4
3
1
1
1
1

关于arrays - 替换索引后的数组项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74496082/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com