how to transfer output in new matrix
1 回表示 (過去 30 日間)
古いコメントを表示
I have a huge [1,565660] matrix.
I have used a FOR loop and an IF/ELSE statement to find some specific data. But I don’t know how to transfer the output into a new matrix of same size as I want to multiple the result later.
For example:Speed=[0,0,0,2,2,2,3,3,3,4,4,4,5,5,6,6,6,7,7,7,6,6,5,5,5,4,4,4,5,5,5,3,3,2,2,2,1,1,1,0,0,0] already have this matrix saved in my workspace.
I need those fprintf outputs as a matrix. How can I do it?
Please help.
for i = 1:length(speed)-1
if speed(1,i)>speed(1,i+1)
fprintf('%d: brake start %d\n %d: %d end \n', i,speed(i),(i+1),speed(i+1));
else
fprintf('index%d: no braking : speed%d\n',i,speed(i));
end
end
4 件のコメント
DGM
2023 年 2 月 8 日
編集済み: DGM
2023 年 2 月 8 日
"I need those fprintf outputs as a matrix."
"i want a matrix that contains the values of if statement and on rest indexes i want zero"
There are no output values being calculated in your if statement. That's why everyone is trying to guess what you want. What values do you want?
Do you want the values from speed when braking occurs?
Do you want the values from speed when no braking occurs?
Do you want a logical vector describing the braking state?
Do you want a bunch of text in an array?
If you have a huge dataset, why are you printing hundreds of thousands of lines of text to console? It only slows things down and you'll never be able to read it all anyway.
In any case, loops shouldn't be necessary.
% locations of braking start
state = [diff(speed)<0 false];
That gives a logical vector of all locations where the "braking start" message is produced. If you want some value from speed, use the logical vector to index into speed.
回答 (1 件)
DGM
2023 年 2 月 8 日
編集済み: DGM
2023 年 2 月 8 日
Here's my guess
speed = [0 0 0 2 2 2 3 3 3 4 4 4 5 5 6 6 6 7 7 7 6 6 5 5 5 4 4 4 5 5 5 3 3 2 2 2 1 1 1 0 0 0];
% locations of braking start
state = [diff(speed)<0 false];
% speed values before braking event
sp0 = zeros(size(speed));
sp0(state) = speed(state);
% speed values after braking event
sp1 = zeros(size(speed));
sp1(state) = speed(circshift(state,1));
% show what the results are
[speed; state; sp0; sp1]
% or if you want text output, use sprintf()/fprintf()
% pick whatever relevant values you want
statenames = {'inactive','active'};
outputelements = [num2cell(speed); statenames(state+1)];
fprintf('Speed is %d; Braking is %s\n',outputelements{:})
3 件のコメント
DGM
2023 年 2 月 8 日
編集済み: DGM
2023 年 2 月 8 日
If your input has 565660 elements, why does the output have 571618 elements? If that's a mistake and they're supposed to be the same length, then these two statements are contradictory:
- "i want both the value when braking occurs and when is not."
- "so when there are no brakes apply there must be 0 in the index"
Statement 1 is equivalent to the input. Statement 2 is equivalent to sp0.
The same thing, but with a more continuous input:
speed = [10 10 10 9 8 7 6 5 4 4 4 4 4 5 5 6 6 5 4 3 2 1 1 1 1 1 ];
% locations of braking start
state = [diff(speed)<0 false];
% speed values before braking event
sp0 = zeros(size(speed));
sp0(state) = speed(state);
% speed values after braking event
sp1 = zeros(size(speed));
sp1(state) = speed(circshift(state,1));
% show what the results are
[speed; state; sp0; sp1]
The only reason I'm concatenating the outputs is to make sure they're easier to read. You don't need to use them in this matrix format.
参考
カテゴリ
Help Center および File Exchange で Brakes and Detents についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!