The_Learner
The_Learner

Reputation: 619

Splitting a list into multiple sub-lists, changing a particlular sub-list, and then rejoining all the sub-lists to form into a single new list?

I have to write a code for doing the following steps.

  1. Loading a file and saving the contents of a file into a list. (the file contains one column and 1223 rows of numbers, say in Matlab code 1:1:1223)
  2. Automatically Splitting the list into multiple sub-lists. (say each sub-list with 100 numbers, i.e. 1223/100 = 12.23 (approximately 13 sub-lists))
  3. Increase the step-size in a particular sublist (say sub-list no. 3 contents from 201:1:300 to 201:0.5:300).
  4. After changing a particular sub-list, joining all the list to form a single list.

I have done the first step and I am struggling to do the steps 2, 3 and 4. I am unable to find a way to implement in tcl (I am a newbie for TCL). I have created a prototype in MATLAB (esp. to explain my need in the StackOverflow community). Following is the code I've written in MATLAB. But Ultimately, I have to implement this in TCL. (Using the cell concept in MATLAB, the whole process got easier)

clc
clear all
close all

Data_Initial = 1:1:1223;
Elements_in_List = 100;

% Dividing the list into Multiple sub-lists

Total_SubLists = floor(length(Data_Initial)/Elements_in_List);

if ((Total_SubLists*Elements_in_List) < length(Data_Initial))
    Total_SubLists = Total_SubLists + 1;
end

for i = 1: Total_SubLists

    Sublist_Begin_RowID = (i - 1)* Elements_in_List + 1;
    Sublist_End_RowID = i * Elements_in_List;

    if (i < Total_SubLists)
        SubLists{i} = Data_Initial(:, Sublist_Begin_RowID:Sublist_End_RowID);
    else 
        SubLists{i} = Data_Initial(:, Sublist_Begin_RowID:end, :);
    end
end

% Changing List NO. 3

K = 3;

A = SubLists{K};
A = A(1):0.5:A(end);
SubLists{K} = A;

% Re-Joining all the list to from a New data

Data_New = [];
for i = 1:length(SubLists)
    Data_New = horzcat(Data_New, SubLists{i});
end

Could anybody please help me do the steps 2, 3 and 4.

Upvotes: 0

Views: 468

Answers (1)

mrcalvin
mrcalvin

Reputation: 3434

Loading a file and saving the contents of a file into a list.

Use the open and split commands.

Automatically Splitting the list into multiple sub-lists.

See this previous answer: Split a list of numbers into smaller list based on a range in TCL

Increase the step-size in a particular sublist

You want to look at Tcl's lset that allows to access and manipulated structures of sub-lists using Tcl's zero-based index expressions.

% set superList [list [list 201 1 300] [list 202 1 300] [list 203 1 300]]
{201 1 300} {202 1 300} {203 1 300}
% lset superList 2 1 0.5
{201 1 300} {202 1 300} {203 0.5 300}

After changing a particular sub-list, joining all the list to form a single list.

One way to achieve this is the combined use of concat and the (parse-time) expansion operator {*}:

% concat {*}$superList
201 1 300 202 1 300 203 0.5 300

But this also depends on the levels of nesting in your super list: See two level splatter TCL

Upvotes: 1

Related Questions