Home > database >  How to make a for loop stop after a set number of iterations in Julia
How to make a for loop stop after a set number of iterations in Julia

Time:10-16

I am still pretty new at programming and I would appreciate any help on how to approach the following problem:

Given a matrix (3x5)

a = [1 2 3 4 5; 
     6 7 8 9 10; 
     11 12 13 14 15;]
  1. I want to iterate through every row
  2. For each row, I want each element to be checked
  3. With each element, I want to store a separate array that holds the element and the next 2 elements.

Ex:

Row 1 = [1 2 3 4 5]
For element 1
return newArray = [1 2 3]

For element 2
return newArray = [2 3 4]

Getting stuck on part 3. How to make the for loop check only up to the next 2 elements and then continue to the next element in the row.

CodePudding user response:

I think that you have some problems with the statement of what you want to achieve. That can often make a programming assignment much harder.

Restating what you have already:

  1. I want to iterate through every row

This is pretty easy

for row = size(a)[1]
 ...
end
  1. For each row, I want each element to be checked

This is where things begin to get squishy? What do you mean by "checked". Let's assume you have some function called checkElement.

  1. With each element, I want to store a separate array that holds the element and the next 2 elements.

How long do you want that separate array to live? Do you just want to hold 3 elements? Or three elements for every cell of the original (i.e. have a 3x5x3 result for a 3x5 input like you show)

Also, what do you want to do about elements 4 and 5 in each row? What values do you want to use for their "next" elements? You could use missing as a value or NaN. Or you could make the result just not contain the problematic inputs.

If you answer these questions, you are likely to find it much easier to write the code you need.

CodePudding user response:

I took a shot at solving what you asked for, but I agree with the others that you need to think more about what you are trying to do and what you want your output to look like. Your request does not sound like something a beginner programmer would realistically use. I am not sure what shape you want to store your "separate array"s in. I have options below for keeping them in a vector or in the original shape of a.

function nexttwo(row, i)
    newarray::Vector{Any} = [row[i]]
    for j=1:2
        i =1
        if length(row) >= i
            push!(newarray, row[i])
        else
            push!(newarray, nothing)
        end
    end
    return newarray
end    

function collectnexttwo(a)
    result_collection = []
    for i in axes(a,1)
        for j in axes(a,2)
            row = a[i,:]
            newarray = nexttwo(row, j)
            push!(result_collection, newarray)
        end
    end
    return result_collection
end

function restoreshape(v, a)
    permutedims(reshape(v, reverse(size(a))))
end
julia> a = [1 2 3 4 5; 6 7 8 9 10; 11 12 13 14 15;]
3×5 Matrix{Int64}:
  1   2   3   4   5
  6   7   8   9  10
 11  12  13  14  15

julia> result = restoreshape(collectnexttwo(a), a)
3×5 Matrix{Any}:
 Any[1, 2, 3]     Any[2, 3, 4]     Any[3, 4, 5]     Any[4, 5, nothing]    Any[5, nothing, nothing]
 Any[6, 7, 8]     Any[7, 8, 9]     Any[8, 9, 10]    Any[9, 10, nothing]   Any[10, nothing, nothing]
 Any[11, 12, 13]  Any[12, 13, 14]  Any[13, 14, 15]  Any[14, 15, nothing]  Any[15, nothing, nothing]
  • Related