/    Sign up×
Community /Pin to ProfileBookmark

convert matrix to arrays

Hi, how do I use PHP to conver the colums of a matrix A to arrays A1, A2, A3…as followed? ?

Matrix A =
1 2 3 4
4 3 2 1
5 6 7 8
8 7 6 5
1 2 3 4

The resultant arrays are:
A1 = [0]=>1 [1]=>4 [2]=>5 [3]=>8 [4]=>1
A2 = [0]=>2 [1]=>3 [2]=>6 [3]=>7 [4]=>2
A3 = [0]=>3 [1]=>2 [2]=>7 [3]=>6 [4]=>3
A4 = [0]=>4 [1]=>1 [2]=>8 [3]=>5 [4]=>4

Thank you very much ?

to post a comment
PHP

19 Comments(s)

Copy linkTweet thisAlerts:
@LiLcRaZyFuZzYJul 21.2006 — where is the matrix? in a file?
Copy linkTweet thisAlerts:
@bokehJul 21.2006 — [code=php]<?php

function matrix_columns($matrix)
{
$columns = array();

// clear out any whitespace (optional but your matrix contains whitespace)
$matrix = preg_replace('/((?!r?n)s)+/', '', $matrix);

// split into rows
$rows = preg_split("/r?n/", $matrix);

// swap the row and column indices
for($i = 0, $length = strlen($rows[0]); $i < $length; $i++)
{
for($j = 0, $count = count($rows); $j < $count; $j++)
{
// if a row is short add null in the blank spots
$columns[$i][$j] = isset($rows[$j][$i]) ? $rows[$j][$i] : null;
}
}
return $columns;
}

// test it
$matrix = <<<END
1 2 3 4
4 3 2 1
5 6 7 8
8 7 6 5
1 2 3 4
END;


header('Content-Type: text/plain');
print_r(matrix_columns($matrix));

?>[/code]
That give you one multidimensional array.[CODE]Array
(
[0] => Array
(
[0] => 1
[1] => 4
[2] => 5
[3] => 8
[4] => 1
)

[1] => Array
(
[0] => 2
[1] => 3
[2] => 6
[3] => 7
[4] => 2
)

[2] => Array
(
[0] => 3
[1] => 2
[2] => 7
[3] => 6
[4] => 3
)

[3] => Array
(
[0] => 4
[1] => 1
[2] => 8
[3] => 5
[4] => 4
)

)[/CODE]
Copy linkTweet thisAlerts:
@yunfannyauthorJul 21.2006 — Hi, LiLcRaZyFuZzY. Yes, the matix is in a text file.

So, bokeh, how do i load the text file containing the matrix and store it as $matrix? Using file_get_contents()? ?

Thank you ?
Copy linkTweet thisAlerts:
@LiLcRaZyFuZzYJul 21.2006 — Using file_get_contents()?[/QUOTE]
exactly,

[code=php]
//...
$matrix = file_get_contents("matrix.txt");
//...
[/code]
Copy linkTweet thisAlerts:
@bokehJul 21.2006 — Hi, LiLcRaZyFuZzY. Yes, the matix is in a text file.

So, bokeh, how do i load the text file containing the matrix and store it as $matrix? Using file_get_contents()? ?

Thank you ?[/QUOTE]


Well knowing that it would be possible to simplify the code and use file(). But if this works ok... if not post a link to the text file.
Copy linkTweet thisAlerts:
@yunfannyauthorJul 21.2006 — the text file is just simplly as:

1 2 3 4

4 3 2 1

5 6 7 8

8 7 6 5

1 2 3 4
Copy linkTweet thisAlerts:
@bokehJul 21.2006 — the text file is just simplly as:

1 2 3 4

4 3 2 1

5 6 7 8

8 7 6 5

1 2 3 4[/QUOTE]
Does it really have all those spaces in it? Are all the numbers always one digit? Are they always numbers?
Copy linkTweet thisAlerts:
@LiLcRaZyFuZzYJul 21.2006 — matrix elements are always numbers, they can be complex,

how are complex numbers handled in PHP?
Copy linkTweet thisAlerts:
@bokehJul 21.2006 — matrix elements are always numbers, they can be complex,

how are complex numbers handled in PHP?[/QUOTE]
What I'm saying is if the numbers are more than one digit the matrix would need a delimiter.
Copy linkTweet thisAlerts:
@LiLcRaZyFuZzYJul 21.2006 — the delimiter would be the space between the numbers i guess
Copy linkTweet thisAlerts:
@bokehJul 21.2006 — Well I thought that was garbage and that is why I asked to see the text file. Loading from a file would be a lot easier anyway... if we ever get to see it!
Copy linkTweet thisAlerts:
@LiLcRaZyFuZzYJul 21.2006 — i guess it is like that, with those same numbers:

[upl-file uuid=c179c172-5013-4527-87dd-519c08ddd92e size=43B]matrix.txt[/upl-file]
Copy linkTweet thisAlerts:
@Vectorman211Jul 22.2006 — You can use any number of spaces as the delimiter, eg. preg_split("/ +/",$line);
Copy linkTweet thisAlerts:
@bokehJul 22.2006 — You can use any number of spaces as the delimiter, eg. preg_split("/ +/",$line);[/QUOTE]How can you presume to know how the file located on the original poster's website is formated?
Copy linkTweet thisAlerts:
@LiLcRaZyFuZzYJul 22.2006 — obviously the OP declared it like that:

the text file is just simplly as:

1 2 3 4

4 3 2 1

5 6 7 8

8 7 6 5

1 2 3 4[/QUOTE]
Copy linkTweet thisAlerts:
@bokehJul 22.2006 — No he didn't. There was one space not multiple ones and he didn't reply to say it was intentional.
Copy linkTweet thisAlerts:
@Vectorman211Jul 22.2006 — 
How can you presume to know how the file located on the original poster's website is formated?[/quote]


I don't presume to know anything about how his file is formatted. I'm just simply stating a fact. ?
Copy linkTweet thisAlerts:
@yunfannyauthorAug 01.2006 — Hi, Guys,

I am so sorry that I have not posted any reply yet. Well, the original matrix just contained simple numbers, but now the new one contains decimal numbers. The attached is the text file. Now the codes seems not working for complex matrix like this ?

[upl-file uuid=1058b7c6-e857-4075-a1ce-9152ee2dcc1b size=91kB]term_topic1_1.txt[/upl-file]
Copy linkTweet thisAlerts:
@bokehAug 01.2006 — [code=php]function matrix_columns($file)
{
$columns = array();
$rows = file($file);
foreach($rows as $key => $row)
{
$rows[$key] = preg_split('/s+/', trim($row));
}
for($i = 0, $length = count($rows[0]); $i < $length; $i++)
{
for($j = 0, $count = count($rows); $j < $count; $j++)
{
$columns[$i][$j] = @$rows[$j][$i];
}
}
return $columns;
}[/code]
×

Success!

Help @yunfanny spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...