how to transfer output in new matrix

1 回表示 (過去 30 日間)
Umang Babubhai Rabari
Umang Babubhai Rabari 2023 年 2 月 8 日
編集済み: DGM 2023 年 2 月 8 日
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 件のコメント
Umang Babubhai Rabari
Umang Babubhai Rabari 2023 年 2 月 8 日
Thank you for your reply. But i want a matrix that contains the values of if statement and on rest indexes i want zero. I want a matrix of same size with elements of if statement and rest zeros
DGM
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
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]
ans = 4×42
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 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 7 0 6 0 0 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 6 0 5 0 0 4 0 0 0 0 0
% 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{:})
Speed is 0; Braking is inactive Speed is 0; Braking is inactive Speed is 0; Braking is inactive Speed is 2; Braking is inactive Speed is 2; Braking is inactive Speed is 2; Braking is inactive Speed is 3; Braking is inactive Speed is 3; Braking is inactive Speed is 3; Braking is inactive Speed is 4; Braking is inactive Speed is 4; Braking is inactive Speed is 4; Braking is inactive Speed is 5; Braking is inactive Speed is 5; Braking is inactive Speed is 6; Braking is inactive Speed is 6; Braking is inactive Speed is 6; Braking is inactive Speed is 7; Braking is inactive Speed is 7; Braking is inactive Speed is 7; Braking is active Speed is 6; Braking is inactive Speed is 6; Braking is active Speed is 5; Braking is inactive Speed is 5; Braking is inactive Speed is 5; Braking is active Speed is 4; Braking is inactive Speed is 4; Braking is inactive Speed is 4; Braking is inactive Speed is 5; Braking is inactive Speed is 5; Braking is inactive Speed is 5; Braking is active Speed is 3; Braking is inactive Speed is 3; Braking is active Speed is 2; Braking is inactive Speed is 2; Braking is inactive Speed is 2; Braking is active Speed is 1; Braking is inactive Speed is 1; Braking is inactive Speed is 1; Braking is active Speed is 0; Braking is inactive Speed is 0; Braking is inactive Speed is 0; Braking is inactive
  3 件のコメント
DGM
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:
  1. "i want both the value when braking occurs and when is not."
  2. "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]
ans = 4×26
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 0 0 1 1 1 1 1 1 0 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0 0 10 9 8 7 6 5 0 0 0 0 0 0 0 0 6 5 4 3 2 0 0 0 0 0 0 0 9 8 7 6 5 4 0 0 0 0 0 0 0 0 5 4 3 2 1 0 0 0 0 0
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.
Umang Babubhai Rabari
Umang Babubhai Rabari 2023 年 2 月 8 日
sorry input is 565660

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeBrakes and Detents についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by