Hello, i am in need of a little guidance, i hope some of you can lead me in the right direction.
I am trying to write a program that stores the coloum number in a cell. I have an array X which contains 15000x14, and i want to store all the values that are between 0 and 1.
I have written a tiny matrix to illustrate our case:
X =
0.1 3 0.2
2 5 4
1 0.3 5
%The output should look something like this with a 0 to describe when the value is between 0 and 1
1 3
0
2
In some cases there are multiple values between 0 and 1 in the same coloum and I want to store them all.
My current line of code for looks like this, and it doesnt work apropriate:
[x,y] =find(0 < X & X < 1);

6 Comments

Luna
Luna on 21 Mar 2019
You have X with 3x3 and how do you think you get output like below?
3 different outputs which is 1x2 and 1x1 and 1x1?
1 3
0
2
Do you want to get the values between 0 and 1 or do you want to replace the values between 0 and 1 with zero?
I might have been unclear in my illustration. The purpose is to note where in the matrix the value is between 0 and 1. So i want to get the values between 0 and 1, i dont want to replace them.
You want to get the values or the locations ?
We want to get the locations.
Sorry for being so unclear in my explanation.
@Walter Roberson
Is:
output = accumarray(x, y, [], @(v) {v});
your suggestion to find to the locations? I have trouble understanding it though.. :/
When you use find() with two outputs, the first output is row numbers and the second output is corresponding column numbers of the locations found.
accumarray(x, y, [], @(v) {v}) is saying to use the row numbers as a grouping variable, and for each different value of x, create an internal list of corresponding y values. Then once everything is grouped, then run the function @(v) {v} on each of those lists, which wraps the list into a cell array.
The effect is the same as
maxx = max(x);
output = cell(maxx, 1);
for K = 1 : maxx
output{K} = Y(x == K);
end

Sign in to comment.

 Accepted Answer

Morten Jørgensen
Morten Jørgensen on 26 Mar 2019

1 vote

stepX={};
for j= 1:size(X,2)
j;
stepX{size(stepX,2)+1} = find(0 < X(:,j) & X(:,j) < 1);
end

More Answers (2)

output = accumarray(x, y, [], @(v) {v});
Luna
Luna on 21 Mar 2019
Edited: Luna on 21 Mar 2019
Try this:
X = [0.1 3 0.2; 2 5 4;1 0.3 5];
[xlocs,ylocs] = find(X<1 & X>0); % locations of values, you have 3 values. xlocs' first element is the row number, ylocs' first element is the column number. Both gives you the location of first value which is between 0-1. Same goes for second and third elements.
vals = nan(1,numel(xlocs)); % preallocation for values
for i = 1:numel(xlocs)
vals(i) = X(xlocs(i),ylocs(i)); % this gets you the values in x on that locations. So vals has 3 elements in this example.
end
%% At the end you can create a table like this:
myTable = table(vals',xlocs,ylocs, 'VariableNames', {'Values','XLocs','YLocs'});
myTable is like below:
myTable =
3×3 table
Values XLocs YLocs
______ _____ _____
0.1 1 1
0.3 3 2
0.2 1 3

4 Comments

They want the locations, not the values.
If all they wanted was a vector of the values, then they would get that with
X(0 < X & X < 1)
Luna
Luna on 21 Mar 2019
Edited: Luna on 21 Mar 2019
XLocs and YLocs are already row and column numbers of the corresponding value's location.
I edited my answer. For example: 0.3 is between 0-1 and its location is 3rd row and 2nd column in the X matrix.
What they are asking for is something that has one row for each row in X, and the columns should reflect the per-row index of where the valid values were found.
The output you build can be done without loops:
[xlocs, ylocs] = find(0 < X & X < 1);
idx = sub2ind(size(X), xlocs, ylocs);
output = [X(idx), xlocs, ylocs];
Luna
Luna on 22 Mar 2019
Ah OK now I got it. Sorry for misunderstanding and thanks for explanation. Your solution works well :) +1

Sign in to comment.

Products

Release

R2018b

Community Treasure Hunt

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

Start Hunting!