Live
Black Hat USADark ReadingBlack Hat AsiaAI BusinessManaged vs Self Hosted Database: Which Is Better for Your Startup?DEV CommunityYour Pipeline Is 22.2h Behind: Catching Finance Sentiment Leads with PulsebitDEV CommunityA Faster Way to Build MongoDB Queries VisuallyDEV CommunityBuy Rating on MNTN: Scalable Self-Serve CTV Platform, Generative AI Innovation, and Underpenetrated SMB Opportunity Drive Multi-Year Growth Potential - TipRanksGoogle News: Generative AIMoving WeOutside246 from GPT-5 to local models on a base M4 Mac MiniDEV CommunityTypeScript Type GuardsDEV CommunityHow Publish a Power BI report and Embed it into a WebsiteDEV CommunityUX Roundup: OpenAI Usability | Integrated Software | Seedance 2 vs. Kling 3 | Grok Imagine | Increasing AI Use - Jakob Nielsen on UXGoogle News: OpenAILetters: As a former English teacher, I know why using ChatGPT on college applications is wrong - sfchronicle.comGoogle News: ChatGPTWas Anthropic’s Claude Source Code Leak Actually a Marketing Stunt?DEV CommunityOpenAI calls for robot taxes, a public wealth fund, and a 4-day workweek to tackle AI disruptionBusiness InsiderGlobal Variable VS Local VariableDEV CommunityBlack Hat USADark ReadingBlack Hat AsiaAI BusinessManaged vs Self Hosted Database: Which Is Better for Your Startup?DEV CommunityYour Pipeline Is 22.2h Behind: Catching Finance Sentiment Leads with PulsebitDEV CommunityA Faster Way to Build MongoDB Queries VisuallyDEV CommunityBuy Rating on MNTN: Scalable Self-Serve CTV Platform, Generative AI Innovation, and Underpenetrated SMB Opportunity Drive Multi-Year Growth Potential - TipRanksGoogle News: Generative AIMoving WeOutside246 from GPT-5 to local models on a base M4 Mac MiniDEV CommunityTypeScript Type GuardsDEV CommunityHow Publish a Power BI report and Embed it into a WebsiteDEV CommunityUX Roundup: OpenAI Usability | Integrated Software | Seedance 2 vs. Kling 3 | Grok Imagine | Increasing AI Use - Jakob Nielsen on UXGoogle News: OpenAILetters: As a former English teacher, I know why using ChatGPT on college applications is wrong - sfchronicle.comGoogle News: ChatGPTWas Anthropic’s Claude Source Code Leak Actually a Marketing Stunt?DEV CommunityOpenAI calls for robot taxes, a public wealth fund, and a 4-day workweek to tackle AI disruptionBusiness InsiderGlobal Variable VS Local VariableDEV Community
AI NEWS HUBbyEIGENVECTOREigenvector

Matrices in Python

DEV Communityby Gaurav YadavApril 5, 20262 min read1 views
Source Quiz

To solve compute problems we use different type of data structures like array , maps, etc. One such data structure is matrices or 2D arrays. Matrices act as foundation for other data structure like graphs . Define matrices in python matrix = [ [ 1 , 2 , 3 ], [ 4 , 5 , 6 ], [ 7 , 8 , 9 ] ] Define a 3x3 matrix in python, row(R) equals 3 and column(C) equals 3 matrix_3x3 = [[ 0 ] * 3 for in range ( 3 )] There are popular problems in matrices: Transpose a matrix: def calculate_transpose_of_matrix ( mat : list [ list ]) -> list [ list ]: N = len ( mat ) for i in range ( N ): for j in range ( i + 1 , N ): mat [ i ][ j ], mat [ j ][ i ] = mat [ j ][ i ], mat [ i ][ j ] return mat Print matrix in spiral pattern: def print_matrix_in_spiral ( mat : list [ list ]) -> list [ list ]: R = len ( mat ) C

To solve compute problems we use different type of data structures like array , maps, etc. One such data structure is matrices or 2D arrays. Matrices act as foundation for other data structure like graphs .

Define matrices in python

matrix = [  [1, 2, 3],  [4, 5, 6],  [7, 8, 9] ]

Enter fullscreen mode

Exit fullscreen mode

Define a 3x3 matrix in python, row(R) equals 3 and column(C) equals 3

matrix_3x3 = [[0]* 3 for in range(3)]*

Enter fullscreen mode

Exit fullscreen mode

There are popular problems in matrices:

Transpose a matrix:

def calculate_transpose_of_matrix(mat: list[list]) -> list[list]:  N = len(mat)  for i in range(N):  for j in range(i+1, N):  mat[i][j], mat[j][i] = mat[j][i], mat[i][j]

return mat`

Enter fullscreen mode

Exit fullscreen mode

Print matrix in spiral pattern:

def print_matrix_in_spiral(mat: list[list]) -> list[list]:  R = len(mat)  C = len(mat[0])

top = 0 left = 0 bottom = R - 1 right = C - 1

while top <= bottom and left <= right: for i in range(left, (right + 1)): print(mat[top][i], end=" ") top += 1 for i in range(top, (bottom + 1)): print(mat[i][right], end=" ") right -= 1 if top <= bottom: for i in range(right, (left - 1), -1): print(mat[bottom][i], end=" ") bottom -= 1 if left <= right: for i in range(bottom, (top - 1), -1): print(mat[i][left], end= " ") left += 1`

Enter fullscreen mode

Exit fullscreen mode

Was this article helpful?

Sign in to highlight and annotate this article

AI
Ask AI about this article
Powered by Eigenvector · full article context loaded
Ready

Conversation starters

Ask anything about this article…

Daily AI Digest

Get the top 5 AI stories delivered to your inbox every morning.

Knowledge Map

Knowledge Map
TopicsEntitiesSource
Matrices in…DEV Communi…

Connected Articles — Knowledge Graph

This article is connected to other articles through shared AI topics and tags.

Knowledge Graph100 articles · 193 connections
Scroll to zoom · drag to pan · click to open

Discussion

Sign in to join the discussion

No comments yet — be the first to share your thoughts!

More in Analyst News