cancel
Showing results for
Did you mean:
cancel
Showing results for
Did you mean:

SOLVED
Highlighted

## Is there a way to extract a specific range of rows and columns from a matrix?

In Matlab, when I plan to extract a submatrix from a large matrix in the following situation: Say I want to extract row and column 1 and 4 from K, I can create an index matrix and do this:

idx = [4 1; 1 2; 2 3; 3 5];

k = K(idx(1,:),idx(1,:));

But in Mathcad, the built in function submatrix can only extract continuous row and column ranges.

Is there a way to let Mathcad do the same job as in Matlab?

Best

Shawn

Tags (4)
1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted

## Re: Is there a way to extract a specific range of rows and columns from a matrix?

So far I guess the following should help. You can extract the appropriate row of your idx matrix using Primes row selector. 15 REPLIES 15
Highlighted

## Re: Is there a way to extract a specific range of rows and columns from a matrix?

That is not supported as a built in function. You can program it though.

Luc

Highlighted

## Re: Is there a way to extract a specific range of rows and columns from a matrix?

This discussion might help...

Highlighted

## Re: Is there a way to extract a specific range of rows and columns from a matrix?

 Say I want to extract row and column 1 and 4 from K,

You are not just looking for the extraction of a single matrix element, or are you?

Or do you really mean what you write - extracting the full first and fourth column and also the first and fourth row. This would mean that in your example you want to extract 16 values and four of them would be extracted twice. If that is what you are after, you would have to tell us in which way you would like the 16 (or 12) value being arranged - which data structure.

In case you are looking for a way to extract a couple of matrix elements whose indices are given by a matrix "idx" and collect those elements in a vector, the following two methods should help. Otherwise you would have to come back and specify more detailed what kind of result you are expecting.

BTW, the value 5 in your idx matrix seems to be wrong anyway, even if you set ORIGIN to 1. Highlighted

## Re: Is there a way to extract a specific range of rows and columns from a matrix?

 Werner Exinger wrote: Say I want to extract row and column 1 and 4 from K, You are not just looking for the extraction of a single matrix element, or are you? Or do you really mean what you write - extracting the full first and fourth column and also the first and fourth row. This would mean that in your example you want to extract 16 values and four of them would be extracted twice. If that is what you are after, you would have to tell us in which way you would like the 16 (or 12) value being arranged - which data structure. In case you are looking for a way to extract a couple of matrix elements whose indices are given by a matrix "idx" and collect those elements in a vector, the following two methods should help. Otherwise you would have to come back and specify more detailed what kind of result you are expecting. BTW, the value 5 in your idx matrix seems to be wrong anyway, even if you set ORIGIN to 1.

‌5 would be wrong as an index into K as given; however, the example he gives is the Cartesian product of the first row of idx (that is, combinations of 4 and 1), so Matlab will return a 2x2 array of those elements.  I've got a few functions that will do vaguely what he wants ... I'll dig them out tomorrow when I've got access to the PC I've got them on.I

A Proper indexing system would be so helpful in cases like this. 😞

Stuart

Highlighted

## Re: Is there a way to extract a specific range of rows and columns from a matrix?

Thank you, Stuart.

I posted a similar question before and I remember you answered me on that.

I really appreciate that.

For this homework, I used Matlab to assemble the matrix and it is pretty handy that in Mathcad Prime I can paste the matlab code to demonstrate the process.

Sorry that I lost the function you gave me last time, I changed my computer but I have created a folder in One Drive so that I can always keep the helps from community back up.

I would agree on that Matlab is more flexible in terms of programming, but this semester after my professor pushes the request for units, I found Mathcad really powerful.

Luckily I am not doing Finite Element in Mathcad so that I don't have to assembly a lot.

Thank you guys so much for sparing your time helping the community grow!

Best

Shawn

Highlighted

## Re: Is there a way to extract a specific range of rows and columns from a matrix?

Hi, Werner,

Sorry I truncate the original problem that results in some confusion.

The original one is this: Thus there are 10 DOFs.

With Matlab, I can do this to assemble the matrix:

k = @(L,EI) [12 -12 6*L 6*L;
-12 12 -6*L -6*L;
6*L -6*L 4*L^2 2*L^2;
6*L -6*L 2*L^2 4*L^2]*EI/L^3; % Beam Local Stiffness Matrix

k1 = k(1/4,1); % beam local stiffness matrice
% Assume EI=1, L=1/4;

K = zeros(10,10); % In total there are 10 DOFs
idxc = [4 1 9 6;
1 2 6 7;
2 3 7 8;
3 5 8 10];
% Index for beam elemental DOFs

for i = 1:4 % Assemble global beam stiffness matrix
K(idxc(i,:),idxc(i,:)) = K(idxc(i,:),idxc(i,:)) + k1;
end

Ktt = K(1:5,1:5) - (K(6:10,1:5))'*(K(6:10,6:10))^-1*K(6:10,1:5);
%Stiffness Condensation

Ktt = Ktt(1:3,1:3); % Subtract Boundary Conditions & Rotational DOFs

Here idxc stores all the 4 DOFs for each element, with the DOFs not continuous labeled.

In Mathcad, however, the built in function submatrix cannot work in this way.

Hope this clarify the problem.

Best

Shawn

Highlighted

## Re: Is there a way to extract a specific range of rows and columns from a matrix?

> Hope this clarify the problem.

Unfortunately not.

Instead of addressing the obviously wrong index number 5 in your post and instead of simply typing in in Mathcad Prime the result you expect given the very example you gave in your last post to show what you are looking for, you again elaborate on what you could and would do in Matlab, introduce new examples which look like they have not much to do with the one you originally gave and introduce something called DOF (what the hell is a DOF?). Why do you think this would clarify anything?

Highlighted

## Re: Is there a way to extract a specific range of rows and columns from a matrix? Sorry for the confusion. Hope this one might work...

Best

Shawn

Highlighted

## Re: Is there a way to extract a specific range of rows and columns from a matrix?

Ah, so you think he is looking for something like this? 