Clear Filters
Clear Filters

Trying to extract three values in one column based on a value in other column

1 view (last 30 days)
Hi,
I have two columns one is 1, NaN, NaN,NaN,NaN,NaN,NaN,2,NaN,NaN,NaN,NaN,NaN,NaN,,3 (...to 11, and then back to 1..2..etc.). The second column has numeric values at the second, fourth and sixth NaN position of each "set". I want to have these values from the second column in a row "beside" the corresponding number from column 1. For example 1; 10,16,18. 2; 5, 6,8 ...... to then ultimately have all the "1s" in a group with their three values, all the 2s in a group etc.
I am struggeling with this. I have tried doing a for loop i=1; while i<size(data, 1), then if data(:,1) == i , i = i + 6; else i = i + 1. It doesnt work.
Could someone give me some ideas or help please :)

Answers (1)

David Hill
David Hill on 20 Oct 2022
newMatrix=[yourMatrix(~isnan(yourMatrix(:,1)),1),reshape(yourMatrix(~isnan(yourMatrix(:,2)),2),3,[])'];

Community Treasure Hunt

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

Start Hunting!