i have a big matrix with 2000x2000
to solve this equation
i need to use only a bandes matrix or half of bandes
i need te have only a bandes matrix without aij equal zero
i found sparse but i dont inderstand how to use it
i need to found this
for exemple
a=[
9 -2 0 -2 0 0 0 0
-2 9 -2 0 -2 0 0 0
0 -2 9 -2 0 -2 0 0
-2 0 -2 9 -2 0 -2 0
0 -2 0 -2 9 -2 0 -2
0 0 -2 0 -2 9 -2 0
0 0 0 -2 0 -2 9 -2
0 0 0 0 -2 0 -2 9]

1 Kommentar

Image Analyst
Image Analyst am 7 Okt. 2020
Not sure what you want.
  1. Do you want a new a where the value is 1 inside your boundaries, essentially a binary map or image of what elements are inside the shape you drew?
  2. Or do you want to "erase" (replace by 0) half the "bande"? If so, which half: the lower or upper half of that shape should be zeroed out?
  3. Or do you want a list of (row, column) coordinates of inside the shape?
Please define exactly what "half of bandes" means to you.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

John D'Errico
John D'Errico am 7 Okt. 2020
Bearbeitet: John D'Errico am 7 Okt. 2020

1 Stimme

You have 5 non-zero bands. Use spdiags. Yes, you could use sparse. But since it is a banded matrix, use a tool to breate a banded matrix.
n = 2000;
A = spdiags(repmat([-2 -2 9 -2 -2],[n 1]),[-3 -1 0 1 3],n,n);
Did it work?
full(A(1:10,1:10))
ans = 10×10
9 -2 0 -2 0 0 0 0 0 0 -2 9 -2 0 -2 0 0 0 0 0 0 -2 9 -2 0 -2 0 0 0 0 -2 0 -2 9 -2 0 -2 0 0 0 0 -2 0 -2 9 -2 0 -2 0 0 0 0 -2 0 -2 9 -2 0 -2 0 0 0 0 -2 0 -2 9 -2 0 -2 0 0 0 0 -2 0 -2 9 -2 0 0 0 0 0 0 -2 0 -2 9 -2 0 0 0 0 0 0 -2 0 -2 9
So the 10x10 square in the upper left corner looks right.
spy(A(1:50,1:50))
And it is indeed banded properly.
You will need to learn to use tools like sparse, and I hope, learn them quickly if you are working with sparse matrices to any extent. But learn to use the proper tools to solve your problem.

3 Kommentare

alize beemiel
alize beemiel am 7 Okt. 2020
thank U sir,
for yours answer
but perhaps i dont explaine my problem clearly
there is my code
my english is not very good
i attashed my matrix
i use this matrix just for exemple cause all my matrix witch i use are very importante
here i give the small one just for exemple
so
KG is matrix i want to banded
for resolve the equation
KG x DD equal FG
some time when i use sholesky or resolving directly i have probleme in singularity or symetrie when i check it
my code is ..
%global KG FG
load KG.txt
load FG.txt
tic
% here i try to arround my matrix perhaps a long decimal make problem
% sorry my english is not well
% so a minimaze the decimale to 7 or 5 or but the problem of symetrie and
% singularity is not solved
% A=KG;
%
% Ndecimals = 7;
% f = 10.^Ndecimals;
% A = round(f*A)/f;
% what ever ... i do mu own cholesky to solve my equation
% all my purpose is to reduce the time of calculeted
% cause this matrix is built for a structure with only 70 nodes its meane
% in 3D .... 70*3 here the time of solved is small
% but with anothers structure with 2000 nodes so a matrixe 2000*3 x 2000*3
% its take a very long time and i know my problem is inside calcul a mij null
%
%
%
%
%
% % symmetrie
% t = issymmetric(A)
% if t == 0
%
% fprintf('Non-symmetrique ');
% return
% end
%
% % singularité
%
% r = rank(A)
% %max(size(A))*eps(norm(A))
% if r < n
% disp('problem singularité');
% return
% end
% LTKG=chol(KGA);
% LKG=LTKG';
% cause my matrix not define positif and singuliarity
% i built my choelesky
[~, n]=size(KG);
%KGA=sparse(KG); % if i use sparse
KGA=KG; % if not use sparse ,
spy(KG(1:n,1:n))
% Factorisation de Cholevsky
mm=length(KGA);
LKG = zeros(mm,mm);
%NNOD
for ij=1:mm
LKG(ij,ij) = sqrt(KGA(ij,ij)-(LKG(ij,:) * LKG(ij,:)'));
for k = (ij+1):mm
LKG(k,ij) =(KGA(k,ij)-(LKG(ij,:)* LKG(k,:)'))/LKG(ij,ij);
end
end
%LKG
%
%
%
%resolution par factorisation du deplacemnent
MY=zeros(3*NNOD,1);
for ik=1 : NNOD*3
s=0;
for in=1:ik-1
s=s+LKG(ik,in)* MY(in);
end
MY(ik)=(FG(ik)-s)/LKG(ik,ik);
end
for jk= NNOD*3:-1:1
s=0;
for jn=jk+1:NNOD*3
s=s+LKG(jn,jk)*DEP(jn);
end
DEP(jk)=(MY(jk)-s)/LKG(jk,jk);
end
DD=DEP' % vecteur de deplacemet avec choeleski
%timeElapsed = toc
...
and thanks
John D'Errico
John D'Errico am 7 Okt. 2020
Bearbeitet: John D'Errico am 7 Okt. 2020
So your question has absolutely nothing to do with what you asked, which was apparently how to create a banded matrix? You did say you did not know how to use sparse.
Instead, your problem seems to be the singularity of a matrix, maybe?
Why in the name of god and little green apples are you writing your own cholesky algorithm? Well written, bug free code is not good enough for you, so you decided to write your own vastly slower running and possibly buggy code?
Seriously, it is literally impossible for me to know where the problem is, because I still have no real clue what is the problem. You don't want or need to write your own Cholesky code. This is just actively a bad idea. A 2kx2k matrix is relatively small, especially a banded one. For example...
n = 2000;
A = spdiags(repmat([-2 -2 9 -2 -2],[n 1]),[-3 -1 0 1 3],n,n);
condest(A)
ans = 17.0000
timeit(@() chol(A))
ans = 5.5138e-04
So the 5-banded matrix I created as a 2000x2000 matrix is well conditioned and symmetric. A Cholesky factor will exist. Is that a slow thing to compute using chol? NOOOOOO!!!!!! It required a tiny fraction of a second to compute the cholesky factorization.
So the very first things you need to learn are:
  1. How to build and manipulate sparse matrices. That means to use sparse and spdiags to start.
  2. How to use the EXISTING tools in MATLAB to work with sparse matrices. Your own code, written to factorize such a matrix will almost certainly be SLOWER then the existing tools in MATLAB. Don't write your own code to perform basic operations.
  3. Since it seems like you have issues with what you THINK is a singular matrix, then learn about tools in MATLAB to determine if a matrix is singular. In terms of sparse matrices, that might be condest as your first choice. However, these are NOT large matrices you are working with, but in fact relatively small matrices. So you could as easly use tools such as full, cond and rank, even svd to learn about your matrices.
Learn these things BEFORE you proceed any further. Don't worry about what is wrong with the code you wrote. THROW IT OUT. Then start over. Use the proper tools to build and factorize your matrices.
alize beemiel
alize beemiel am 7 Okt. 2020
thanks sir,
merci pour votre patience ! .

Melden Sie sich an, um zu kommentieren.

Kategorien

Tags

Gefragt:

am 7 Okt. 2020

Kommentiert:

am 7 Okt. 2020

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by