How can i sort data using logical indexing?

조회 수: 6 (최근 30일)
Timo Merker
Timo Merker 2016년 1월 7일
댓글: Timo Merker 2016년 1월 7일
problem: i have two arrays: the first one is of unknown lenght and has two columns, one containing the data that i want to add to my second array and one containing a natural number (ranging from 1 to unknown) looking like this:
[ 0] [3]
[ 0] [5]
[ 3.36] [ 2]
[ 0.00100000000000122] [ 6]
[0.000999999999999446] [ 4]
[0.000999999999999446] [ 2]
my second array is empty. now i want to add the values (first columns) to the associated row ( second column) in my second array. There are multiple values for each row. the output of the second array should look like this:
[] []
[3.36] [0.00099999999446]
[0] []
[0.0009999999999446] []
[0] []
[0.00100000000000122] []
Is there a way to do this using logical indexing? It would be nice if i could avoid using for loops with if statements
Thanks.
  댓글 수: 2
the cyclist
the cyclist 2016년 1월 7일
Can you write explicitly what you want the output to be, for this example?
Timo Merker
Timo Merker 2016년 1월 7일
sorry i hit the enter button before i was done. added the output!
Thanks

댓글을 달려면 로그인하십시오.

채택된 답변

Guillaume
Guillaume 2016년 1월 7일
Redistributing your numbers in the row indicated by the second column is very easy with accumarray:
a = [0 3
0 5
3.36000000000000 2
0.00100000000000122 6
0.000999999999999446 4
0.000999999999999446 2];
b = accumarray(a(:, 2), a(:, 1), [], @(v) {v.'})
This creates a 1d (column) cell array where each row is all the numbers corresponding to that particular row index. Each row is just long enough to store the required numbers
The problem comes from you wanting a 2d cell array instead. To be honest, I'm not sure there's any advantage in it and it requires more work. One way of generating it:
%continuing from above
maxcols = max(cellfun(@numel, b)); %or you could get that from the histogram of a(:, 2)
c = cellfun(@(v) [num2cell(v), cell(1, maxcols - numel(v))], b, 'UniformOutput', false);
c = vertcat(c{:})
  댓글 수: 3
Guillaume
Guillaume 2016년 1월 7일
It's important to use valid syntax in your question to avoid confusion. From your description, I assumed that the input array is a standard array (i.e. matrix), not a cell array.
Now, why is your input a cell array? and more importantly, why does it have empty cells in the first column?
If the cell is empty, it's not going to contribute anything to the output, so you might as well remove that row. Once the input cell array has no empty rows, you can just convert it to a matrix and my answer will work:
a = {0 3
0 5
[] 3
3.36000000000000 2
0.00100000000000122 6
0.000999999999999446 4
0.000999999999999446 2}; %a is now a cell array not a matrix
a(any(cellfun(@isempty, a), 2), :) = []; %remove rows with empty values
a = cell2mat(a)
%now you can use accumarray
Timo Merker
Timo Merker 2016년 1월 7일
you are right, i didnt use the correct syntax. I'm new to matlab so im still struggeling with this. Im dealing with a huge dataset and though it should not contain empty cells i wasnt shure. checked it, fixed it, converted to matrix like you said and it works perfectly! So many thanks for your time and patience, i appreciate it! :-)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Identification에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by