create y using x, without loop
显示 更早的评论
size x = 2,3
size y = 1,157
y(3:52,1) = 1; y(53:102) = 2;... I need this but in one line of code that doesn't involve a loop.
X is dynamically growing because it is pulling data from another array. In X, columns 1&2 represent the index of rows needed for Y; however, column 3 is data inputted to Y for each of those rows respectively. Columns 1&2 will always be ordered consecutively column 3 will not.
x =
[ 3 52 1
53 102 2
103 157 98]
y =
[0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 98 98 98...]
3 个评论
Adam Danz
2019-5-8
Problem 1: you haven't explained the transformation. How do you get from x to y (in words)?
Problem 2: x seems to be a [2-by-3] matrix. What is the size of y? It has missing values so is it a cell array?
Jebidiah Light
2019-5-8
采纳的回答
更多回答(1 个)
Steven Lord
2019-5-8
This is complicated code, but it does satisfy the "one line, no loop" requirement. You will need to substitute 102 for the desired length of the vector and [3 53] with the first column of x.
y = cumsum(subsasgn(zeros(1, 102), substruct('()', {[3 53]}), 1))
You'd probably want to break it apart into pieces to understand what this line is doing, then add a paragraph of comments before putting this line in your code so the next person reading the code can understand it.
The phrase "for loop" is not (necessarily) a four letter word in MATLAB. Use for if it's the right tool for the job, use something else if that other tool is the right one.
10 个评论
madhan ravi
2019-5-8
Thank you :)
Really interesting. While digging into it, I realized that both of our no-loop approaches would break if the indices were not consecutive. For example,
x = [ 3 52 1
60 102 2]; %52 not consecutive with 60
I updated my no-loop solution so that it works with discontinuous indices.
Jebidiah Light
2019-5-9
@Jebidiah Light, maybe you're not using my most recent version (I added a comment under my answer yesterday to let you know I updated it).
When I run the for-loop and my no-for-loop versions, they produce the same results. Neither version depend on there being any consecutive values in any column of x.
x = [
103 602 5
603 1102 11
1103 1602 26
1603 2102 34
2103 2602 39
2603 3102 42
3103 3602 43
3603 4102 55
4103 4602 56
4603 5102 59
5103 5602 67
5603 6102 68
6103 6602 78
6603 7102 79
7103 7602 96
7603 8102 98 ];
% For loop method
y = zeros(1,max(max(x(:,[1,2]))));
for i = 1:size(x,1)
y(x(i,1):x(i,2)) = x(i,3);
end
% no for loop method
xc = mat2cell(x,ones(1,size(x,1)),size(x,2));
yc = cellfun(@(x)ones(1,x(2)-x(1)+1).*x(3), xc, 'UniformOutput',false);
xIdx = cellfun(@(x)x(1):x(2), xc,'UniformOutput', false);
y2 = zeros(1,max(max(x(:,[1,2]))));
y2(cell2mat(xIdx')) = cell2mat(yc');
% Compare results
isequal(y,y2)
% ans =
% logical
% 1
Jebidiah Light
2019-5-9
Steven Lord
2019-5-9
I suspect I know what you want with the data you posted, but can you clearly state (in words) exactly what each column of x represents and how it relates to the desired output? If possible give us a clear recipe rather than feeding us a sample cookie and asking us to make one just like it.
Jebidiah Light
2019-5-9
Adam Danz
2019-5-9
My 3rd solution is listed first in the answer (just FYI).
Jebidiah Light
2019-5-9
类别
在 帮助中心 和 File Exchange 中查找有关 Variables 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!