3

I have a matrix like

frequencyarray =

    697    697    697    697    697    697      0      0    697
      0      0      0      0      0      0    770    770      0
      0      0      0      0      0      0      0      0      0
      0      0      0      0      0      0      0      0      0
   1209   1209   1209   1209   1209   1209   1209   1209   1209
      0      0      0      0      0      0      0      0      0
      0      0      0      0      0      0      0      0      0

I'd now like to eliminate all the zeros on a vector base from it and get this

frequencyarray2 =

    697    697    697    697    697    697    770    770    697
   1209   1209   1209   1209   1209   1209   1209   1209   1209

I only want to do this if exactly two non-zero numbers are in a vector. If a vector has more or less than two non-zero numbers, they should be replaced with zeros.

Therefore, if I have something like:

frequencyarray =

      0      0    697    697
      0      0    770    770
      0      0      0      0
      0      0      0      0
   1209   1209   1209   1209
      0      0      0      0
      0      0      0      0

the response should be:

frequencyarray2 =

      0      0      0      0

Using find, this doesn't work, it just does everything in on single vector. I thought about using find and then reshape. But that does only work if exactly two numbers are non-zero per vector.

If possible, I'd like to avoid loops. The columns don't have any relation between them.

Any idea is appreciated

3
  • Not a simple problem - are you sure you want/must solve it vector based?
    – bdecaf
    Jan 21, 2012 at 15:29
  • hmm well... that's just the output of the previous part. i could think about changing it though. i guess i'll just use a for if there's no matlab-like-magic way to do this Jan 21, 2012 at 15:36
  • 1
    well it's two part. as I see it you can treat the lines vectorized - but you still have to iterate over it line - by line. so a "half-vectorized" approach is possible. Also Ali showed you you find "candidate" lines vectorized.
    – bdecaf
    Jan 21, 2012 at 15:40

3 Answers 3

2

You can use sum and find together. Something like:

frequencyarray(sum(frequencyarray==0)==2)
1
  • actually you shouldn't using find in that case. Instead logical indexing. Your solution will sum up indexes found in find - only way to achieve 2 is when the only 0 is at index 2.
    – bdecaf
    Jan 21, 2012 at 15:30
1

a helpful code snippet to compare two lines:

all( (frequencyarray(i,:) == 0) & (frequencyarray(i+1,:) ~= 0) ) 

it will be true when all the 0 are non-zero in the following line. The line comparison is vectorized - though you have to move through the array.

To join the lines you could use:

i_cand = (frequencyarray(i,:) == 0
joined_line = frequencyarray(i,:); % start
joined_line(~icand) = frequencyarray(i+1,~icand); % end of joining
0

Well... I've solved the problem. Even not in a way I'd desired, but it works... And it doesn't have to be ultra-fast.

Here's my solution, if someone is interested:

function [collapsed] = collapse(frequencies)
    fsize = size(frequencies,2);
    collapsed = zeros(2,fsize);
    for it=1:fsize
        col = frequencies(:,it);
        nonzero = frequencies(find(col != 0));
        if (size(nonzero,1) == 2)
            collapsed(:,it) = nonzero;
        endif
    endfor
endfunction
3
  • Shouldn't it be nonzero = col(find(col != 0))? Also Matlab has boolean indexing so you can write nonzero = col(col != 0). col != 0 returns a boolean array and A[B] when B is a boolean array the same size as A, returns the elements of A where B is True.
    – Bi Rico
    Jan 21, 2012 at 17:11
  • hmm didn't test that yet. my implementation works and the project's not worth the damn taking a closer look ;) nice comment though. Jan 21, 2012 at 17:29
  • I don't think nonzero = frequencies(find(col != 0)) can work, you're always going to be indexing the first column. I think you either want nonzero = frequencies(find(col != 0), it); or nonzero = col(find(col != 0)).
    – Bi Rico
    Jan 21, 2012 at 17:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.