开发者

replacing a value in all columns with value of that column

How do I replace all the 1s in ALL columns with the number of that column? I can already do it column by column:

output(output(:,3)==1,3) = 3;

output(output(:,4)==1,4) = 4;

output(output(:,5)==1,5) = 5;

etc...

but I feel li开发者_如何学JAVAke a fool writing that out for every single column. There should be a way to do them all at once right?


You can use FIND for this:

[rowIdx,colIdx] = find(output==1);
output(output==1) = colIdx;


I don't quite get what you're trying to do, but you could use a MATLAB for loop.

for i=1:numberOfColumns,
    output(:,i) = i;
end

Is this sort of like you're after...?

Edit:

for x=1:numberOfRows,
    for y=1:numberOfColumns,
        if output(x, y) == 1
            output(x, y) == y
        end
    end
end

By the way I may have the columns/rows the wrong way round because I can't remember which way MATLAB represents its matricies!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜