LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Reshape 2D Array and Remove Zeros in 2D Array

Solved!
Go to solution

Hi All,

 

This is a two part question. Been wracking my brain for ages with this and came up with a couple of ideas that have came as close as I can get to the expected output.

 

1) Is there a possible way of changing how Reshape Array places the n+1 element?

 

Normally, as an example, 1D to 2D Reshape Array gives:

1 2 3 4 5 6 7 8      -> 1 2 3 4      instead of   ->  1 3 5 7

                                  5 6 7 8                              2 4 6 8

 

2) Is there a good way of removing zeros from a 2D array?

 

An example is attached with an attempt at a solution and expected outcome.

0 Kudos
Message 1 of 15
(2,108 Views)

@SDuffyLV wrote:

Hi All,

 

This is a two part question. Been wracking my brain for ages with this and came up with a couple of ideas that have came as close as I can get to the expected output.

 

1) Is there a possible way of changing how Reshape Array places the n+1 element?

 

Normally, as an example, 1D to 2D Reshape Array gives:

1 2 3 4 5 6 7 8      -> 1 2 3 4      instead of   ->  1 3 5 7

                                  5 6 7 8                              2 4 6 8

 

2) Is there a good way of removing zeros from a 2D array?

 

An example is attached with an attempt at a solution and expected outcome.


1)  Nom the function won't do that.  But you could code it:

a.png

 

2) I don't understand the concept of removing an element from a 2D array/

"If you weren't supposed to push it, it wouldn't be a button."
0 Kudos
Message 2 of 15
(2,076 Views)

Sometimes you simply need to occasionally transpose and correctly connect the reshape sizes.... 😄

 

altenbach_1-1624663616398.png

 

 

 

(Still it all seems a bit hacky: Are we assuming that the arrays cannot have negative values? Why are things orange if we have all integers? Why is the input a 2D instead of a 1D array? Why sort if things are already sorted? Is the size guaranteed to be divisible by four? etc.)

 

What exactly is the purpose of all this? Where does the data come from?

0 Kudos
Message 3 of 15
(2,064 Views)

Also if you remove the zeroes first, the code gets a bit simpler (no loop pyramids, etc.):

 

altenbach_0-1624664827305.png

 

As I already said, it really depends on what we can expect from the data. Why are there zeroes in the first place? 

0 Kudos
Message 4 of 15
(2,055 Views)

Hi Altenbach,

 

Thanks for the reply.

 

Attached is a copy of a more in-depth look into the issue. I've answered a few questions below if the attached vi and txt document doesn't seem clear compared to the original.

 

  1. Why are things orange if we have all integers?
    1. This is just an example of a more in depth problem. The real values are meant to be dbl. See attached for in depth problem
  2. Why is the input a 2D instead of a 1D array?
    1. With the in depth problem, we are reading a delimited spreadsheet. For some reason, the output ends up that way. 
    2. The text file we are reading from (also attached) came from "Copy to clipboard" from another VI (no need to know details about this) and simply pasted to a .txt file.
    3. Reading a delimited spreadsheet on this .txt file comes out as a 1D array
  3. Why sort if things are already sorted?
    1. It was an idea to try to "shift" the non-zero values up to the top of the array
  4. Is the size guaranteed to be divisible by four?
    1. In the sense that there should be 4 columns, all equal size in rows, yes.
  5. Why are there zeroes in the first place? 
    1. This was put as an example in place of the .txt file attached, which has negative numbers.

 

Download All
0 Kudos
Message 5 of 15
(2,018 Views)

Your "expected" output contains stale default data, completely unrelated to the current input.

 

Can you explain the arrangement of the data in the text file. What's the significance of the rows and columns? So all you want is to keep the positive values, right?

 

Doing a reshape on a 2D array will keep the data in flat memory order and just redefine row sizes (and pad and trim as needed). Since your lines end up in an extra tab character, you get a fifth column of zeroes that will get intermixed after reshaping. Does not seem reasonable at all! Maybe you want the subset of the first four column instead. (A subset is very different from reshaping!!!)

 

Your file contains 278 positive values, which is not divisible by four. How should they be arranged?

0 Kudos
Message 6 of 15
(2,008 Views)

@SDuffyLV wrote:
  • Reading a delimited spreadsheet on this .txt file comes out as a 1D array

     


Did you mean to say 2D array?

0 Kudos
Message 7 of 15
(1,997 Views)

Apologies! Forgot to set the expected arrays as default.

 

  1. Did you mean to say 2D array?
    1. Initially it was a 1D array, I had the delimiter as comma instead of tab, so it came out as a 1D array. No need to look into this.
  2. What's the significance of the rows and columns? So all you want is to keep the positive values, right?
    1. Rows is time, columns is arbitrary (different channels on an oscilloscope, let's say)
    2. Yes, I just want to keep the positive values
  3. Fifth column of zeros
    1. I've subset to fix that. No problem now
  4. Your file contains 278 positive values, which is not divisible by four. How should they be arranged?

    1. I'm planning to filter out any values +/- 10% of the median of the positive values.
    2. There will be some more zero values that can just be passed through the delete zeros function again
    3. That will get rid some of the positive values to make it divisible by four
Download All
0 Kudos
Message 8 of 15
(1,991 Views)

If you randomly remove rows, the time information is lost. do you need to keep that information?

What should happen if for any give row only some elements are positive?

0 Kudos
Message 9 of 15
(1,982 Views)
  1. If you randomly remove rows, the time information is lost. do you need to keep that information?

    1. No need for timing information as that's just defined as the row number (e.g. first row is 1 seconds, second row is 2 seconds etc.). We don't care about the timing here.
  2. What should happen if for any give row only some elements are positive?

    1. All columns will have equal number of positive (>0) + filtered (+/-10% of median) elements
    2. I understand if there is an unequal number of positive numbers in the columns then we will have zeros at the bottom for some columns. This is fine because once they're filtered, it will result in an equal number of positive+filtered elements
    3. Perhaps separating each column into a 1D array is a solution as the result of all of this is to mean average the positive+filtered values. Separating them into 1D arrays will remove the additional zeros (if there are any unexpectedly)

Simply, I just don't know how to shift the positive values to the top of the array and delete any zeros at the bottom. It doesn't matter what order the rows are in in each column, that's why one of the ideas was to Sort each column but then removing the zeros is the issue. If I just took the mean, it would include the zeros.

0 Kudos
Message 10 of 15
(1,975 Views)