Skip to main content
1-Visitor
May 8, 2012
Solved

How to extract non zero submatrix to a matrix table

  • May 8, 2012
  • 2 replies
  • 7434 views

test.bmp

Best answer by StuartBruff

Neptali Barte wrote:

Yes, it works! Thanks Raiko. My another problem is that when I add more values in the columns of the 1st row only (See snapshot). It only gets the first row only. I want to include in the results the values of the second to the last row where there is a value even when there are zeroes below the 1st row.

Change your last line to

submatrix(test,min(I),max(I),min(J),max(J))

Stuart

2 replies

12-Amethyst
May 8, 2012

Use submatirx()

tmp12.gif

Bartz871-VisitorAuthor
1-Visitor
May 8, 2012

Yup, the answer is quite correct but my goal is that, I don't want to update or change the value inside the submatrix function everytime I add values to either the rows or colums of "test" table. "test" is actually an excel component that I made with an output range say A1:F100. Anytime, I want to add values in the rows and columns in ascending order, the remaining cells in the range will be zero. An I only wanted to get the values in the cells that have an input in excel component. Do somebody have a function to do this?

17-Peridot
May 8, 2012

Hello Neptali,

this function does the trick. However, if the pattern of the non-zero values is irregular (e.g. interspersed with zeros) it will generate an array that still contains some zeros.

Raiko

Bartz871-VisitorAuthor
1-Visitor
May 8, 2012

I'm using an excel component for the table and I got the below error in red. Why?

test2.bmp

23-Emerald V
May 8, 2012

Neptali Barte wrote:

I'm using an excel component for the table and I got the below error in red. Why?

Because you have i counting columns instead of rows (and j counting rows instead of columns). the index test[i,j identifies row i, column j. Change them round and swap q and p in your submatrix call.

Stuart

PS,

a. There is no need to add the <>0 test after the IsString call; Mathcad treats all non-zero values as true and as IsString evaluates to 1 for a string, then it will be (by definition) not equal to zero. I don't think you need the test anyway, all you're after is non-zero elements and a string is <> 0.

b. There is no need to assign the result of submatrix to W; it's redundant as you don't use W and Mathcad will simply return the value of the submatrix if you remove the W<- bit.